Prevent click event from affecting parent jquery
Asked Answered
B

3

5

I was to stop the event propagation from the child to the parent, i have a bunch of li tags containing a.

$('li a[rel=close]').live('click', function(e){
    e.stopPropagation();
    e.preventDefault();
})

But it doesn;t stop the event.Any suggestions ?

Bundle answered 8/10, 2011 at 10:5 Comment(2)
Does the event fire at least? Can you share your HTML code too?Sophy
possible duplicate of child action doesn't trigger parent jqueryVery
G
3

stopPropagation has problems with live, from the jQuery stopPropagation docs -

Since the .live() method handles events once they have propagated to the top of the document, it is not possible to stop propagation of live events

As Rob W has said your code would work fine with bind, here's a demo - http://jsfiddle.net/TmKyT/

Gerge answered 8/10, 2011 at 10:14 Comment(1)
stopPropagation works for stopping the bubble from triggering ancestor live events, but it will trigger other listeners in the bubble that where bound using bind jsfiddle.net/yEnzCObellia
A
1

Use .bind instead of .live. The live event is triggered at the end of the propagation tree. live is only more useful than bind when you want to also bind the event listener for elements which are created later.

Anny answered 8/10, 2011 at 10:15 Comment(0)
A
0

Maybe try using delegate?

$('ul.parent').delegate('li a[rel=close]', 'click', function( event ) {

}
Abrupt answered 8/10, 2011 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.