Javascript DOM HTML document addEventListener() Method add more than one event handlers

Introduction

You can add many events to the document, without overwriting existing events.

This example demonstrates how to add two click events to the document:

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>Click anywhere in the document.</p>
<p id="demo"></p>
<script>
document.addEventListener("click", myFunction);
document.addEventListener("click", someOtherFunction);

function myFunction() {/*w  ww . ja va2 s  .  co  m*/
  document.getElementById("demo").innerHTML += "Hello World!";
}

function someOtherFunction() {
  document.getElementById("demo").innerHTML += "This function was also executed!";
}
</script>

</body>
</html>



PreviousNext

Related