Javascript DOM HTML document addEventListener() Method add event handler by name

Introduction

You can add events of different types to the document.

This example demonstrates how to add many events to the document:

View in separate window

<!DOCTYPE html>
<html>
<body>

<p>This example uses the addEventListener() method to add many events on the document.</p>

<p id="demo"></p>

<script>
document.addEventListener("mouseover", myFunction);
document.addEventListener("click", mySecondFunction);
document.addEventListener("mouseout", myThirdFunction);

function myFunction() {/*from   ww  w.  j a v  a  2  s.  c  o m*/
  document.getElementById("demo").innerHTML += "Moused over!<br>"
}

function mySecondFunction() {
  document.getElementById("demo").innerHTML += "Clicked!<br>"
}

function myThirdFunction() {
  document.getElementById("demo").innerHTML += "Moused out!<br>"
}
</script>

</body>
</html>



PreviousNext

Related