Skip to content
Related Articles

Related Articles

Call multiple JavaScript functions in onclick event
  • Last Updated : 11 Jun, 2019

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:

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :