Add click event handler with element.addEventListener() method - Javascript DOM

Javascript examples for DOM:Element addEventListener

Description

Add click event handler with element.addEventListener() method

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(){//from  w  w w.j ava 2  s .c  o m
var field = document.getElementById('textfield');
var value = 'Some text';
field.addEventListener("click", function () {
    this.setAttribute('value', value);
});
    }

      </script> 
   </head> 
   <body> 
      <input id="textfield" type="text">  
   </body>
</html>

Related Tutorials