How to work with addEventListener() method in JavaScript HTML DOM?


Use the addEventListener() method to attach an event handler.

Example

You can try to run the following code to learn how to work with addEventListener() method in JavaScript −

<!DOCTYPE html>
<html>
   <body>
      <p>Double click the below button.</p>
      <button id = "btnid"> Double Click </button>
      <p id = "pid"> </p>
      <script>
         document.getElementById("btnid").addEventListener("dblclick", displayEvent);
         function displayEvent() {
            document.getElementById("pid").innerHTML = Date();
         }
      </script>
   </body>
</html>

Updated on: 23-Jun-2020

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements