How to run multiple JavaScript functions onclick

By Saruque Ahamed Mollick

onclick() event is very much useful to us as we can call any function on click event. But you are here to learn how to run multiple functions in JavaScript. So Here I am gonna show you how to run multiple JavaScript functions onclick.

It does not matter how many numbers of functions need to be called. You can run two or multiple numbers of functions using onclick event.

Free cool 3d image hover effect source code download

How To Change Background Color Every Seconds in JavaScript

Run multiple JavaScript functions onclick

onclick is an HTML attribute. This event is triggered when mouse is clicked on it.

This is an example of using onclick event.

<button onclick="any_function()">Click to fire</button>

You can use either function or anything.
Check this below link to learn more



https://www.w3schools.com/jsref/event_onclick.asp

Now, you need to call multiple functions at the same time using onclick event. So how to do that?

It’s very similar to the previous one. Just you need to use the proper syntax.

<button onclick="function1();function2();function3();">Click to fire</button>

Run two JavaScript functions onclick

You just need to follow the same rule

<button onclick="function1();function2();">Click to fire</button>
<button onclick="function1();function2();">Click to fire</button>
<script>
function1(){
alert("hi");
}
function2(){
console.log("this is another function");
}
</script>

This was just an example to make you understood, you can use this method to call more than one function at the same time with onclick event in this way.

You may also read,

How to Pause or Stop Audio After Few Seconds in JavaScript

How to Hide HTML Element onclick in jQuery Easily

 

 

Leave a Reply

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