'Exclude onclick() event from div

I have this code:

<body onclick="myFunction();">
<head>
<!-- some javascript here -->
<body>
<div id="element1">
   <div id="element2">
      <div id="element3">
      </div>
   </div>
</div>
</body>

Now I want to prevent the onClick on <body> triggering if the user clicks inside the <div id="element3"> area.

How I can do this?



Solution 1:[1]

You can exclude the area by hijacking the click handler and calling event.stopPropogation(); (window.event.cancelBubble = true; in older versions of ie)

<div id="element1" onclick="event.stopPropagation(); window.event.cancelBubble = true;">sadsadsadsad
       <div id="element2">                
         <div id="element3"> 
             </div>        
         </div>     
     </div>  
 </body>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1