JavaScript Event Listener Tutorial

The addEventListener() method

The addEventListener() method attaches an event handler to the specified element without overwriting existing event handlers. You can add many event handlers to one element.

Syntax

element.addEventListener(event, function, useCapture);

Example: 1

Add an event listener that fires when a user clicks a button

document.getElementById("myBtn").addEventListener("click", displayDate);

Add an Event Handler to an Element

Example

Alert “Hello World!” when the user clicks on an element

element.addEventListener("click", function(){ alert("Hello World!"); });

Take Your Free Live Demo Class Now

We Will Call Back You Shortly

Enter Your DETAILS BELOW

We Will Send You the Course Fees Details In Below Number

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

Enter Your DETAILS BELOW TO take

free demo class