JAVASCRIPT Tutorial
addEventListener is a method used to attach an event listener to an HTML element, allowing you to specify the type of event and the function to execute when the event occurs.
addEventListener method on the element, specifying the event type and the callback function.// Obtain the button element
const button = document.querySelector("button");
// Define the event type
const eventType = "click";
// Create a callback function
function handleClick() {
alert("Button clicked!");
}
// Attach the event listener
button.addEventListener(eventType, handleClick);
In this example, when the button is clicked, the handleClick function will be executed.