How to call multiple JavaScript functions in an onclick event?

Spread the love

Sometimes, we want to call multiple JavaScript functions in an onclick event.

In this article, we’ll look at how to call multiple JavaScript functions in an onclick event.

How to call multiple JavaScript functions in an onclick event?

To call multiple JavaScript functions in an onclick event, we add an event listener that calls all the functions.

For instance, we write

const btn = document.querySelector(".btn");
btn.addEventListener("click", () => {
  showAlert();
  validate();
  anotherFunction();
  yetAnotherFunction();
});

to call addEventListener to add a click event handler function.

In it, we call showAlert, validate, anotherFunction, and yetAnotherFunction.

Conclusion

To call multiple JavaScript functions in an onclick event, we add an event listener that calls all the functions.

Leave a Reply

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