Dmitry Gladkov
Dmitry Gladkov

Reputation: 1355

Stop jQuery from calling next events

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.

Upvotes: 3

Views: 8560

Answers (5)

user619568
user619568

Reputation: 71

You may also want to look at event.stopImmediatePropagation, as stopPropagation "will not prevent other handlers on the same element from running" (for example, two submits handlers on the same form should still be triggered if using stopPropagation).

Upvotes: 7

Vitor Almeida
Vitor Almeida

Reputation: 81

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

Upvotes: 0

John Strickler
John Strickler

Reputation: 25421

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.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

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

Upvotes: 2

Mottie
Mottie

Reputation: 86413

Use the stopPropagation() method:

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

Upvotes: 5

Related Questions