DEV Community

Rajeshkumar Vijayakumar
Rajeshkumar Vijayakumar

Posted on

addEventListner() Method in Java script

The addEventListener() method is used to attach an event handler to an element without overwriting existing event handlers.

This allows multiple functions to run on the same event

event → The type of event (like "click", "mouseover", "keydown").

function → The function to run when the event occurs (callback function).

useCapture (optional) → Boolean value.

false (default) → Event handled in bubbling phase.

true → Event handled in capturing phase.

Hover over me!

document.getElementById("para").addEventListener("mouseover", function() {
this.style.color = "red";
});

Hoisting

Objects provided by the host environment (not built into the JavaScript language itself).
These are different depending on where JS runs.

In Browser:

window

document

navigator

localStorage, setTimeout, etc.

Top comments (0)