DEV Community

Mohamed Ajmal
Mohamed Ajmal

Posted on

Events and Event Listener in JS.

HTML events are things that happen to HTML elements. Event is an action performed by the user or browser.

Event Listener is a function that listens to that event and executes code.

Examples of Events:

User clicks a button → click

User types → keydown

Page loads → load

Form submits → submit

Event = something that happens.

Event listener

An event listener is a function that waits for an event and responds when the event occurs.

Event listener = code that reacts to an event.

example code:

Click Me

let btn = document.getElementById("btn");

btn.addEventListener("click", () => {
console.log("Button clicked");
});

Top comments (0)