The previous answers all work, but my preferred way is to check $(event.target).closest()
for the non-handled child node in the container node's event handler, to avoid various gotchas. event.stopPropgation()
prevents higher-level containers from seeing the event, which oftentimes ends up being needed. Checking e.target
will fail if there's a nested element underneath sub that's clicked instead of sub directly. This approach avoids both issues:
$(".main").on("click", function (event) {
if($(event.target).closest(".sub").length){
return;
}
alert("main")
});
$(".sub").on("click", function (event) {
alert("sub");
});
Fiddle w/ example of a higher-level container that has an independent event handler.
http://jsfiddle.net/rH5vj/1/