Call multiple JavaScript functions in onclick event - GeeksforGeeks

30 Jun, 2021
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 functions by mentioning their names with the onclick event separated by semi-colon “;”. 
HTML
<!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.
HTML
<!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: 
 
anikakapoor
JavaScript-Misc
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
How to get character array from string in JavaScript?
How to remove duplicate elements from JavaScript Array ?
How to get selected value in dropdown list using JavaScript ?
How to filter object array based on attributes?
Remove elements from a JavaScript Array
How to detect browser or tab closing in JavaScript ?
Lodash _.debounce() Method
Angular File Upload