This is caused by a JavaScript tag called the bbbling event . By default, your events will bubble in the DOM.
When you click on a child element of a node, you automatically fire the click event for it parent node (s).
By default, when an element is clicked, bubbles occur from within : this means that the event of the child click() element will be triggered first, then the parent element, etc.
You can solve the problem by adding a secondary click handler to your child and telling the browser to stop bubbling in a cross-browser compatible way ( see live example ):
<script language="javascript"> function parentHandler(e) { alert('parent clicked'); }; function childHandler(e) { if (!e) var e = window.event; e.cancelBubble = true; if (e.stopPropagation) e.stopPropagation(); alert('child clicked'); }; </script> <div id="parent" onclick="parentHandler(event);"> Foo <div id="child" onclick="childHandler(event)"> Bar </div> </div>
source share