Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop an event propagation in jquery?

Tags:

jquery

ajax

$("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 ..

like image 819
silimer Avatar asked May 17 '11 22:05

silimer


People also ask

How do you stop event propagation?

stopPropagation() The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases. It does not, however, prevent any default behaviors from occurring; for instance, clicks on links are still processed.

How do I stop event propagation in HTML?

stopPropagation() Event Method The stopPropagation() method prevents propagation of the same event from being called. Propagation means bubbling up to parent elements or capturing down to child elements.

What is event bubbling How do you stop it?

If you want to stop the event bubbling, this can be achieved by the use of the event. stopPropagation() method. If you want to stop the event flow from event target to top element in DOM, event. stopPropagation() method stops the event to travel to the bottom to top.

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

stop propagation and prevent default behavior.

$("div#foobar").live('click', function(event) {  
    event.stopPropagation();
    event.preventDefault()
});
like image 174
Adrian Rodriguez Avatar answered Oct 03 '22 11:10

Adrian Rodriguez