How to stop events bubbling in jQuery?

Stann picture Stann · Dec 23, 2010 · Viewed 59k times · Source

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;
})

Answer

Daniel T. picture Daniel T. · Dec 23, 2010

According to jQuery's documentation:

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