You can try this:
HTML
<div onClick="someJScode();" class="parent">
<div class="child" onclick="childCallback(event)"></div>
</div>
JavaScript
function someJScode() {
alert('Click!');
}
function childCallback(event) {
event.stopImmediatePropagation();
return false;
}
DEMO
The code above uses stopImmediatePropagation You can also use stopPropagation
Here is more generic solution:
var preventChildrenPropagation = (function (parentClass) {
var preventPropagation = function (event) {
event.stopPropagation();
return false;
}
return function (parent) {
var foreach = Array.prototype.forEach;
foreach.call(document.getElementsByClassName(parent), function (e) {
foreach.call(e.children, function (c) {
c.onclick = preventPropagation;
});
});
}
}());
preventChildrenPropagation('parent');
The method preventChildrenPropagation
will prevent the propagation of all click
events of all children of the "parent" element.