JAVASCRIPT Tutorial
Multiple event listeners allow an element to respond to multiple events or multiple functions for the same event.
addEventListener()
method to attach the event listeners to the element.Event listeners execute in the order they are added. If multiple event listeners are attached to the same element for the same event, they will execute in that order.
const button = document.getElementById("my-button");
// Add event listener for click event
button.addEventListener("click", () => {
console.log("Clicked");
});
// Add event listener for hover event
button.addEventListener("hover", () => {
console.log("Hovered");
});
In this example:
button
element has two event listeners attached: one for the "click" event and one for the "hover" event.