'How to add multiple different functions on single on submit command in javascript

I have one function for validation,and a second function for message output how I can run both of them together with a single submit click.

var myform = document.forms.hform; // hform is name

myform.onsubmit = validation;

myform.onsubmit = exercise;


Solution 1:[1]

Just wrap them to one function:

myform.onsubmit = (e) => {
  validation(e);
  exercise(e);
}

Solution 2:[2]

You can call functions inside of functions…

function myFunc() {
   validation();
   exercise();
}

<button onsubmit=“myFunc”>

Solution 3:[3]

Modern javascript (by modern in this case I mean since about 2010 or even earlier) you'd use addEventListener

Like this

var myform = document.forms.hform;
myform.addEventListener('submit', validation);
myform.addEventListener('submit', exercise);
myform.addEventListener('submit', youSaidThreeFunctions);

You can add an event listener at any place in your code, so that's better than running the functions in another function

addEventListener is more flexible than using onEvent

Solution 4:[4]

let handleSubmit = (e)=>{
   e.preventDefault();  // prevents default functionality of browser of reloading the webpage
   validation();
   exercise();
   // you can add more lines of code / function to be executed when onsubmit
}
var myform = document.getElementById("respective id of the form tag");

myform.addEventListener('submit', handleSubmit);

Solution 5:[5]

Try this, you can create two individual function where each fuction is called like a stack .

function validation(){
        alert(" exercise() function executed successfully!");
    }
     
    function exercise()(){
        alert(" exercise() function executed successfully!");
    }   
    <button type="button" onclick="sayHello();sayHi();">Click Me</button>

See how the code is functioning Here

For more you can visit and Here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EzioMercer
Solution 2 matt_jared
Solution 3 Bravo
Solution 4
Solution 5 Manishyadav