form on submit code example

Example 1: jquery submit form

// It is simply
$('form').submit();

// However, you're most likely wanting to operate on the form data
// So you will have to do something like the following...
$('form').submit(function(e){
	// Stop the form submitting
  	e.preventDefault();
  	// Do whatever it is you wish to do
  	//...
  	// Now submit it 
    // Don't use $(this).submit() FFS!
  	// You'll never leave this function & smash the call stack! :D
  	e.currentTarget.submit();
});

Example 2: js form submit listener

document.getElementById("#myFormId").addEventListener("submit", function(e){
    if(!isValid){
        e.preventDefault();    //stop form from submitting
    }
  	//do whatever an submit the form
});

Example 3: html submit form onclick

var form = document.getElementById("form-id");

document.getElementById("your-id").addEventListener("click", function () {
  form.submit();
});

Example 4: how to use form on submit in html

<form onsubmit="myFunction()">
  Enter name: <input type="text">
  <input type="submit">
</form>

Tags:

Html Example