Javascript examples for DOM:Element addEventListener
Add click event handler with element.addEventListener() method
<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>