Element addEventListener() Method - If browsers that don't support addEventListener() method, use the attachEvent() method. - Javascript DOM

Javascript examples for DOM:Element addEventListener

Description

Element addEventListener() Method - If browsers that don't support addEventListener() method, use the attachEvent() method.

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials