Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop events bubbling in jQuery? [duplicate]

How do I stop custom event bubbling in jQuery?

For example I have this code:

$('.myclass').bind('amodaldestroy', function(){     ....does something..... }) 

How do I only allow this to be triggered once on the first element it finds when bubbling? Can I just add return false?

$('.myclass').bind('amodaldestroy', function(){      ....does something.....     return false; }) 
like image 434
Stann Avatar asked Dec 23 '10 20:12

Stann


People also ask

How do I stop jquery from bubbling?

The event. stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed. Tip: Use the event. isPropagationStopped() method to check whether this method was called for the event.

How does jquery handle event bubbling?

The concept of "bubbling up" is like if you have a child element with a click event and you don't want it to trigger the click event of the parent. You could use event. stopPropagation() . event.

Which method prevent the event from bubbling up the DOM tree?

stopPropagation()Returns: undefined. Description: Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.


1 Answers

According to jQuery's documentation:

$('myclass').bind('amodaldestroy'), function(event) {     ....does something....     event.stopPropagation(); }); 
like image 96
Daniel T. Avatar answered Sep 28 '22 08:09

Daniel T.