O'Reilly logo

Async JavaScript by Trevor Burnham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Custom jQuery Events

Custom events are an underappreciated feature of jQuery that make it easy to graft a powerful distributed event system onto any web app, with no additional libraries. You can emit any event you want from any DOM element from jQuery using trigger.

 
$(​'#tabby, #socks'​).on(​'meow'​, ​function​() {
 
console.log(this.id + ​' meowed'​);
 
});
 
$(​'#tabby'​).trigger(​'meow'​); ​// "tabby meowed"
 
$(​'#socks'​).trigger(​'meow'​); ​// "socks meowed"

If you’ve worked with DOM events before, you’re no doubt familiar with bubbling. Whenever an element emits an event (such as a ’click’), its parent then emits the event, then its grandparent, and so on, up to the root element, document—unless the event’s stopPropagation method is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required