views:

35

answers:

4

I write a jQuery plugin the first time and I'm wondering if there is a way to stop jQuery from running the next attached events.

Example:

$(this).submit(function(){
    return $(this).do_some_validation();
}

If validation didn't pass (i.e. the function returned false), the form should not be submitted, but if there are any other event handlers attached, only last handler return value can prevent form from being submitted.

+3  A: 

Use the stopPropagation() method:

$(this).submit(function(e){
    e.stopPropagation();
    return $(this).do_some_validation();
}
fudgey
+2  A: 

You may take a look at the event.stopPropagation() function.

Darin Dimitrov
A: 

stopPropagation() works on most events but may not always work on delegated events, .live() and .delegate().

return false; is a fool proof way if stopPropogation doesn't work first.

John Strickler
A: 

I use this functions with .live() and never have problems

jQuery("a.entryTable, a.entryDelete").live("click", function(e) {   
    e.preventDefault();
    e.stopPropagation();

    // code

    return false;
});
ovitinho