silimer
silimer

Reputation: 73

How to stop an event propagation in jquery?

$("div#foobar").live('click', function() {  

});

$(window).hashchange(function() {   

});

How can I stop firing hashchange event when ever click event fires.

Note On click event I am firing an ajax and changing the hash ... And on hashchange I am doing something. Since Click event also changes the hash so hashevent fires. So, I want that whenever click event fires hashchange should not be fire ..

Upvotes: 6

Views: 2427

Answers (3)

PeeHaa
PeeHaa

Reputation: 72672

(function() {

    var fire_hashchange = true;

    $("div#foobar").live('click', function() {  
        fire_hashchange = false;
        // do stuff
    });

    $(window).hashchange(function() {   
        if (!fire_hashchange) {
            fire_hashchange = true;
            return;
        }
        // do stuff
    });
})();

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237865

Basically, you can't do what you want to do. If you are changing the hash, of course the hashchange event is going to be fired. That's what it's there for. Preventing the propagation of the click event is not going to prevent the handling of the hashchange event.

Design your system so that this isn't a problem.

Upvotes: 1

Adrian Rodriguez
Adrian Rodriguez

Reputation: 3242

stop propagation and prevent default behavior.

$("div#foobar").live('click', function(event) {  
    event.stopPropagation();
    event.preventDefault()
});

Upvotes: 10

Related Questions