I have some jQuery code to close my menu:
$('body').click(function(){
$('#menu').hide();
});
$("#menu").click(function(e) {
e.stopPropagation();
return false;
});
However on my #menu element I have some elements that have click events attached to them:
$('.menu_elem').live("click", function(){
//Do something
});
My problem is that the e.stopPropagation() is preventing my click events for my menu. How can I avoid this?
I’m not sure I understand your HTML exactly, but if
#menuis the root of your menu and.menu_elemare menu items in the menu, then you could change your delegated event handling to to be captures at#menulike this:This has the following advantages:
It changes the delegated event handling to capture the bubbled events at
#menuso it doesn’t need propagation beyond#menuin order to work so you don’t have to change your.stopPropagation()code.It switches away from (the now deprecated)
.live()to the replacement.on()which gives you more control over where the bubbled events are captured.