Use addEventListener to add event handler for click - Javascript DOM Event

Javascript examples for DOM Event:onclick

Description

Use addEventListener to add event handler for click

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from   www  .  j  a va  2  s .co  m
console.log("Connected");
var button = document.querySelector("button");
button.addEventListener("click", function() {
   console.log("Clicked");
   document.body.style.background = "pink";
});
    }

      </script> 
   </head> 
   <body> 
      <button>Click me</button>  
   </body>
</html>

Related Tutorials