Call multiple JavaScript functions in onclick event
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 ?
- JavaScript | Pass string parameter in onClick function
- JavaScript | Function Call
- What is the difference between call and apply in JavaScript?
- How to call function from it name stored in a string using JavaScript?
- JavaScript | Call a function after a fixed time
- JavaScript | Replace multiple strings with multiple other strings
- JavaScript | Responsive time of an event
- How to call a function that return another function in JavaScript ?
- Functions in JavaScript
- Round off a number to the next multiple of 5 using JavaScript
- JavaScript | Nested functions
- Arrow functions in JavaScript
- Array of functions in JavaScript
- Cross-browser window resize event using JavaScript/jQuery
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.



