Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bubble custom events down

Events only bubble up, but sometimes one needs to trigger a custom event on a parent element and make it call all the handlers on any of its children if they have a listener bound to them. What's the best way to do this in jQuery?

like image 516
Charon ME Avatar asked Jan 14 '13 10:01

Charon ME


1 Answers

so far I came up with this:

$(startingParentElement).addBack().find('*').each(function(index,element){ 
    $(element).triggerHandler('myCustomEventName');
});
  • triggerHandler() ensures the event doesn't bubble back up
  • each() is needed because triggerHandler() is only performed on the first item in the matched collection
  • addBack() ensures a handler bound to the startingParentElement is called too
like image 95
Charon ME Avatar answered Sep 21 '22 18:09

Charon ME