0

I have this structure

<div>
    <ul>
        <li>...</li>
        <li....
        ...
    </ul>
</div>

and in Jquery

$(div).click(function(){...});
$(li).click(function(){....});

Now my problem is when I click on li, div click event also firing. how can set to only fire li click event when click on li.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
kirankumar
  • 191
  • 1
  • 2
  • 13
  • 1
    possible duplicate of [Prevent execution of parent event handler](http://stackoverflow.com/questions/1398582/prevent-execution-of-parent-event-handler) and [How to prevent a click() event through an internal div to parent div in jQuery?](http://stackoverflow.com/questions/7835775/how-to-prevent-a-click-event-through-an-internal-div-to-parent-div-in-jquery) and [How do I prevent a parent's onclick event from firing when a child anchor is clicked?](http://stackoverflow.com/questions/1369035/how-do-i-prevent-a-parents-onclick-event-from-firing-when-a-child-anchor-is-cli). – Felix Kling Aug 10 '12 at 10:17

5 Answers5

3
$(div).click(function(){...});
$(li).click(function(ev){ ev.stopPropagation(); /* other code here */ });

Use stopPropagation()

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
3

Do the following:

$(li).click(function(e) {e.stopPropagation()...})
DerWaldschrat
  • 1,865
  • 13
  • 13
2

Return false from the lis event, this will stop it bubbling up the DOM tree.

Update, and for good measure do event.preventDefault() too, event being the first argument passed into the handling function.

Paystey
  • 3,287
  • 2
  • 18
  • 32