Disable form auto submit on button click

JavascriptHtmlButtonForm SubmitForms

Javascript Problem Overview


I have a HTML form where I use several buttons. The problem is that no matter which button I click, the form will get submitted even if the button is not of type "submit". e.g. Buttons like :<button>Click to do something</button>, result in form submission.

It's quite painful to do an e.preventDefault() for each one of these buttons.

I use jQuery and jQuery UI and the website is in HTML5.

Is there a way to disable this automatic behavior?

Javascript Solutions


Solution 1 - Javascript

Buttons like <button>Click to do something</button> are submit buttons.

Set type="button" to change that. type="submit" is the default (as specified by the HTML spec):

> The missing value default and invalid value default are the Submit Button state.

Solution 2 - Javascript

You could just try using return false (return false overrides default behaviour on every DOM element) like that :

myform.onsubmit = function ()
{ 
  // do what you want
  return false
}

and then submit your form using myform.submit()

or alternatively :

mybutton.onclick = function () 
{
   // do what you want
   return false
}

Also, if you use type="button" your form will not be submitted.

Solution 3 - Javascript

<button>'s are in fact submit buttons, they have no other main functionality. You will have to set the type to button.
But if you bind your event handler like below, you target all buttons and do not have to do it manually for each button!

$('form button').on("click",function(e){
    e.preventDefault();
});

Solution 4 - Javascript

if you want to add directly to input as attribute, use this

 onclick="return false;" 

<input id = "btnPlay" type="button" onclick="return false;" value="play" /> 

this will prevent form submit behaviour

Solution 5 - Javascript

Like mas-designs said, call preventDefault(). You can call it on the form itself. Here's a function that does this for all forms, vanilla JS.

function forms_ini(){
  for(var form of document.getElementsByTagName('form')){
    form.addEventListener('submit', function(ev){
      ev.preventDefault()
    })
  }
}

Solution 6 - Javascript

another one:

if(this.checkValidity() == false) {

                $(this).addClass('was-validated');
                e.preventDefault();
                e.stopPropagation();
                e.stopImmediatePropagation();

                return false;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParisView Question on Stackoverflow
Solution 1 - JavascriptQuentinView Answer on Stackoverflow
Solution 2 - JavascriptBoris D. TeoharovView Answer on Stackoverflow
Solution 3 - Javascriptmas-designsView Answer on Stackoverflow
Solution 4 - Javascriptbh_earth0View Answer on Stackoverflow
Solution 5 - Javascriptétale-cohomologyView Answer on Stackoverflow
Solution 6 - JavascriptHello HackView Answer on Stackoverflow