Given multiple functions, the task is to call them by just one onclick event using JavaScript. Here are few methods discussed. Either we can call them by mentioning their names with element where onclick event occurs or first call a single function and all the other functions are called inside that function.
Example 1: This example calls both funtions by mentioning their names with the onclick event separated by semi-colon “;”.
<!DOCTYPE html> <html> <head> <title> JavaScript | call multiple functions in onclick event. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun_1(); GFG_Fun_2()"> click here </button> <p id="GFG_DOWN_1" style="color:green; font-size: 20px; font-weight: bold;"> <p id="GFG_DOWN_2" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = 'Click on the button to call multiple functions'; var down_1 = document.getElementById('GFG_DOWN_1'); var down_2 = document.getElementById('GFG_DOWN_2'); function GFG_Fun_1() { down_1.innerHTML = 'From function 1'; } function GFG_Fun_2() { down_2.innerHTML = 'From function 2'; } </script> </body> </html> |
Output:
-
Before clicking the button:
-
After clicking the button:
Example 2: This example first calls a single function and now it is responsibility of that function to call the others. This example does the same.
<!DOCTYPE html> <html> <head> <title> JavaScript | call multiple functions in onclick event. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun(); "> click here </button> <p id="GFG_DOWN_1" style="color:green; font-size: 20px; font-weight: bold;"> <p id="GFG_DOWN_2" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = 'Click on the button to call multiple functions'; var down_1 = document.getElementById('GFG_DOWN_1'); var down_2 = document.getElementById('GFG_DOWN_2'); function GFG_Fun() { GFG_Fun_1(); GFG_Fun_2(); } function GFG_Fun_1() { down_1.innerHTML = 'From function 1'; } function GFG_Fun_2() { down_2.innerHTML = 'From function 2'; } </script> </body> </html> |
Output:
-
Before clicking the button:
-
After clicking the button:
Recommended Posts:
- How to stop event propagation with inline onclick attribute in JavaScript ?
- Call by Value Vs Call by Reference in JavaScript
- JavaScript | Pass string parameter in onClick function
- How to change an element color based on value of the color picker value using onclick?
- JavaScript | Call a function after a fixed time
- How to call a function that return another function in JavaScript ?
- How to call a function repeatedly every 5 seconds in JavaScript ?
- How to make GET call to an API using Axios in JavaScript?
- How to make POST call to an API using Axios.js in JavaScript ?
- How to call a parent method from child class in JavaScript?
- How to return the response from an asynchronous call in JavaScript ?
- JavaScript | Responsive time of an event
- Cross-browser window resize event using JavaScript/jQuery
- How to wait resize end event and then perform an action using JavaScript ?
- How to get the class of a element which has fired an event using JavaScript/JQuery?
- Phases of JavaScript Event
- What are the microtask and macrotask within an event loop in JavaScript ?
- Event bubbling in JavaScript
- Functions in JavaScript
- Higher-Order Arrow Functions in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


