0

I have this odd problem where i have an UL with some LI elements in it.. I have bound a dblclick() event to both the UL and the LI, and when I dblclick the LI element, both the LI event and the UL event is triggered.. is there a way to avoid this?

This is my code:

    $("ul").dblclick(function () {
        alert("ul clicked");
    });

    $("li").dblclick(function () {
        alert("li clicked");
    });
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35
Oldenborg
  • 906
  • 3
  • 13
  • 26

1 Answers1

3

The dblclick event bubbles up the DOM tree, and ancestor elements are also notified. To prevent the event from bubbling, you need to stop propagation of the event:

$("li").dblclick(function (e) {
    alert("li clicked");
    e.stopPropagation();
});

Example: http://jsfiddle.net/HkUQ4/

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307