Handle onclick event for child elements - Javascript DOM Event

Javascript examples for DOM Event:onclick

Description

Handle onclick event for child elements

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*  ww  w  .j  a v  a  2 s  .c o m*/
var childs = document.getElementById('link').children;
for (var i = 0; childs[i]; i++) {
    childs[i].onclick = function () {
        this.style.color = "#ff0000";
    }
}
    }

      </script> 
   </head> 
   <body> 
      <div id="link"> 
         <p>text1</p> 
         <p>text1</p> 
         <p>text1</p> 
      </div>  
   </body>
</html>

Related Tutorials