Javascript DOM HTML Element addEventListener() Method

Introduction

This example demonstrates how to execute a function when a user clicks on a <button> element:

This example uses the addEventListener() method to execute a function when a user clicks on a button.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button id="myBtn">Test</button>

<p id="demo">

<script>
document.getElementById("myBtn").addEventListener("click", myFunction);

function myFunction() {//w w w  .  ja v  a  2  s  . c o  m
  document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>



PreviousNext

Related