Computer >> Computer tutorials >  >> Programming >> Javascript

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>