Javascript examples for DOM:Element addEventListener
Element addEventListener() Method - If browsers that don't support addEventListener() method, use the attachEvent() method.
<!DOCTYPE html> <html> <body> <button id="myBtn">Test</button> <script> var x = document.getElementById("myBtn"); if (x.addEventListener) {// w ww. j a v a2 s . c o m x.addEventListener("click", myFunction); } else if (x.attachEvent) { x.attachEvent("onclick", myFunction); } function myFunction() { console.log("Hello World!"); } </script> </body> </html>