Assign click event handler to all element - Javascript DOM Event

Javascript examples for DOM Event:onclick

Description

Assign click event handler to all element

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body>
      <!--<input id="boton" type="button" value="virtual cl">--> 
      <p id="boton">pBoton</p> 
      <script>
var clickHandler = function(){
  console.log('clicked!');
};
var elements = document.getElementsByTagName('p');

for(var i = 0; i<elements.length; i++){
  elements[i].onclick = clickHandler;
}

      </script>  
   </body>//from  ww w  .j av a 2  s  .  c o  m
</html>

Related Tutorials