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 “;”.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Before clicking the button:
  • After clicking the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.