3

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?

0

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you have to handle IE with e.cancelBubble = true as well, or does jQuery handle this?
@peirix - jQuery normalises the event object so you don't need to worry.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.