Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery Event.stopPropagation() seems not to work

Tags:

jquery

events

Am I totally missing what this is supposed to do? I expect that if I call stopPropagation() on an event, handlers for that event won't get triggered on ancestor elements, but the example below isn't working that way (in FireFox 3 at least)..

<script type="text/javascript">
    $("input").live("click", function(event){
        console.log("input click handler called")
        event.stopPropagation()
    });

    $("body").live("click", function(event){
        console.log("body was click handler called. event.isPropagationStopped() returns: " + event.isPropagationStopped());
    })

</script>

 ...

<body>
    <input type="text" >
</body>
like image 220
morgancodes Avatar asked Jul 13 '09 22:07

morgancodes


1 Answers

Live events don't follow the same event bubbling rules. See the documentation on live event handling.

Quote from reference above:

Live events do not bubble in the traditional manner and cannot be stopped using stopPropagation or stopImmediatePropagation. For example, take the case of two click events - one bound to "li" and another "li a". Should a click occur on the inner anchor BOTH events will be triggered. This is because when a $("li").bind("click", fn); is bound you're actually saying "Whenever a click event occurs on an LI element - or inside an LI element - trigger this click event." To stop further processing for a live event, fn must return false.

like image 194
tvanfosson Avatar answered Nov 12 '22 15:11

tvanfosson