What are events and event listeners in JavaScript?

An event is something that happens on the page, like a click, a key press or a form submit. An event listener is a function that runs when the event happens.

button.addEventListener("click", (event) => {
  console.log("Clicked: " + event.target.id);
});

You can add many listeners to one event. See DOM EventListener.


Learn more about the similar topics:
Tutorials
JavaScript Events
JavaScript HTML DOM
JavaScript DOM Manipulation
JavaScript HTML DOM EventListener
JavaScript Forms and Validation
Exercises & Assignments
Write a JavaScript program to change the text of a paragraph on button click
Write a JavaScript program to switch dark mode on and off
Interview Questions & Answers
What is the DOM in JavaScript?
How do you select elements in the DOM?
What are events and event listeners in JavaScript?
What is event bubbling and capturing in JavaScript?
What is event delegation in JavaScript?