Categories
JavaScript Answers

How to call multiple JavaScript functions in onclick event?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *