Leon van der Veen
Leon van der Veen

Reputation: 1672

jquery click with nested div

I have a div and when I click, it opens a menu. In that div i have a nested div, and when I click on, it opens the same menu which is also logical.

Now I want when I press the nested div it doesn't open the menu.

How can I do that?

Upvotes: 3

Views: 1318

Answers (3)

Jayendra
Jayendra

Reputation: 52809

Use event.stopPropagation or event.stopImmediatePropagation to prevent the event from bubbling.

Upvotes: 0

xdazz
xdazz

Reputation: 160943

Use event.stopPropagation() to prevents the event from bubbling up the DOM tree.

Upvotes: 0

James Allardice
James Allardice

Reputation: 166041

You can bind a click event handler to the inner div and stop the propagation of the event there:

$("#innerDiv").click(function(e) {
    e.stopPropagation();
});

That will prevent the event from bubbling up to the parent element, where it would cause the click event handler bound to it to fire.

Upvotes: 4

Related Questions