DEV Community

Cover image for How to create Custom Events in JavaScript?

How to create Custom Events in JavaScript?

Madhu Saini on April 09, 2024

Introduction Imagine you're building a complex web application. Pre-built events like "click" and "submit" are great, but what if you ne...
Collapse
 
florianrappl profile image
Florian Rappl

Without any reference to bubbling or the ability to cancel events you could have also just called it "events". What distinguishes DOM events (i.e., in-built and custom events) from ordinary events are, however, those special abilities. So it would be great to also list them.

Collapse
 
madhusaini22 profile image
Madhu Saini

Thanks for the insightful perspective! You're absolutely right about the unique characteristics of DOM events, like bubbling and the ability to cancel them, setting them apart from ordinary events

Collapse
 
thekadur profile image
thekadur • Edited

Hello Madhu! This is an inspiring article, and we at Codemischief Software Solutions would love to repost it on our official page with due credits for more reach.
We are Codemischief Software Development Solutions and I am the Strategy personnel at the firm.

Would love to connect!
LinkedIn : [(linkedin.com/company/codemischief-...]
Website : [(codemischief.tech/)]
Personal : [(linkedin.com/in/manish-kadur)]

Regards,
Manish Kadur

Collapse
 
madhusaini22 profile image
Madhu Saini

That's amazing!!

Thank you so much for the support!, this really motivate me to do more good work :)

Collapse
 
spykelionel profile image
Ndi Lionel

I have been writing JavaScript for some time now but this is the first time I am witnessing custom events. This article is so mind-blowing. JavaScript is so powerful when you know how to use it. Thanks for this.

Collapse
 
madhusaini22 profile image
Madhu Saini

I really appreciate your kind words <3

Thank you so much for reading :)

Collapse
 
firecrow8 profile image
Stephen Firecrow Silvernight

WOW! that is nuts and I like it. this is definitely food for thought.

I've always like the event dispatcher model (of running the queue when any JS reaches global scope).

Thank you for posting this, the ability to route events through the DOM node tree is really cool!

Collapse
 
madhusaini22 profile image
Madhu Saini

Thanks for reading :)

Collapse
 
programordie profile image
programORdie

I didn't knew this was possible, thank you for writing this!

Collapse
 
madhusaini22 profile image
Madhu Saini

Glad you've learned something new!

Thanks for the feedback :)

Collapse
 
stefanak-michal profile image
Michal Štefaňák

Does it have to be attached to DOM node or it can be used on ordinary class?

Collapse
 
madhusaini22 profile image
Madhu Saini

Nope, innerText and textContent are meant for DOM nodes, not regular classes. They're perfect for updating text content in HTML elements using JavaScript.

Collapse
 
cipivlad profile image
Cipi

Thanks for sharing! I like this one 👌

Collapse
 
madhusaini22 profile image
Madhu Saini

Glad you liked it :)

Collapse
 
bhavy-wot profile image
Bhavy

Good one.

Collapse
 
madhusaini22 profile image
Madhu Saini • Edited

Thanks, Bhavy!!

Collapse
 
skipperhoa profile image
Hòa Nguyễn Coder

Thanks you!