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 ?

andrei
  • 8,252
  • 14
  • 51
  • 66
  • Does the event fire at least? Can you share your HTML code too? – Soufiane Hassou Oct 08 '11 at 10:14
  • possible duplicate of [child action doesn't trigger parent jquery](http://stackoverflow.com/questions/6975946/child-action-doesnt-trigger-parent-jquery) – AXMIM May 13 '15 at 14:10

3 Answers3

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/

ipr101
  • 24,096
  • 8
  • 59
  • 61
  • ``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** http://jsfiddle.net/yEnzC/ – David Hellsing Oct 08 '11 at 10:55
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.

Rob W
  • 341,306
  • 83
  • 791
  • 678
0

Maybe try using delegate?

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

}
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
moleculezz
  • 7,513
  • 4
  • 25
  • 25