To call multiple JavaScript functions in onclick event, we use addEventListener
.
For instance, we write
const el = document.getElementById("id");
el.addEventListener("click", () => {
alert("hello world");
});
el.addEventListener("click", () => {
alert("another event");
});
to select the element with getElementById
.
Then we call addEventListener
to add a click listener to the element.
We call it twice with difference functions to add all the functions as click listeners.
So they’ll all be called when we click on the element.