Disable submit button on form submit

JavascriptJquery

Javascript Problem Overview


I wrote this code to disable submit buttons on my website after the click:

$('input[type=submit]').click(function(){
    $(this).attr('disabled', 'disabled');
});

Unfortunately, it doesn't send the form. How can I fix this?

EDIT I'd like to bind the submit, not the form :)

Javascript Solutions


Solution 1 - Javascript

Do it onSubmit():

$('form#id').submit(function(){
    $(this).find(':input[type=submit]').prop('disabled', true);
});

What is happening is you're disabling the button altogether before it actually triggers the submit event.

You should probably also think about naming your elements with IDs or CLASSes, so you don't select all inputs of submit type on the page.

Demonstration: http://jsfiddle.net/userdude/2hgnZ/

(Note, I use preventDefault() and return false so the form doesn't actual submit in the example; leave this off in your use.)

Solution 2 - Javascript

Specifically if someone is facing problem in Chrome:

What you need to do to fix this is to use the onSubmit tag in the <form> element to set the submit button disabled. This will allow Chrome to disable the button immediately after it is pressed and the form submission will still go ahead...

<form name ="myform" method="POST" action="dosomething.php" onSubmit="document.getElementById('submit').disabled=true;"> 
     <input type="submit" name="submit" value="Submit" id="submit"> 
</form>

Solution 3 - Javascript

Disabled controls do not submit their values which does not help in knowing if the user clicked save or delete.

So I store the button value in a hidden which does get submitted. The name of the hidden is the same as the button name. I call all my buttons by the name of button.

E.g. <button type="submit" name="button" value="save">Save</button>

Based on this I found here. Just store the clicked button in a variable.

$(document).ready(function(){
    var submitButton$;
	
    $(document).on('click', ":submit", function (e)
	{
		// you may choose to remove disabled from all buttons first here.
		submitButton$ = $(this);
	});
    
	$(document).on('submit', "form", function(e)
	{
		var form$ = $(this);
		var hiddenButton$ = $('#button', form$);
		if (IsNull(hiddenButton$))
		{
			// add the hidden to the form as needed
			hiddenButton$ = $('<input>')
				.attr({ type: 'hidden', id: 'button', name: 'button' })
				.appendTo(form$);
		}
		hiddenButton$.attr('value', submitButton$.attr('value'));
		submitButton$.attr("disabled", "disabled");
    }
});

Here is my IsNull function. Use or substitue your own version for IsNull or undefined etc.

function IsNull(obj)
{
	var is;
	if (obj instanceof jQuery)
		is = obj.length <= 0;
	else
		is = obj === null || typeof obj === 'undefined' || obj == "";

	return is;
}

Solution 4 - Javascript

This should take care of it in your app.

$(":submit").closest("form").submit(function(){
    $(':submit').attr('disabled', 'disabled');
});

Solution 5 - Javascript

Simple and effective solution is

<form ... onsubmit="myButton.disabled = true; return true;">
    ...
    <input type="submit" name="myButton" value="Submit">
</form>

Source: here

Solution 6 - Javascript

A more simplier way. I've tried this and it worked fine for me:

$(':input[type=submit]').prop('disabled', true);

Solution 7 - Javascript

Want to submit value of button as well and prevent double form submit?

If you are using button of type submit and want to submit value of button as well, which will not happen if the button is disabled, you can set a form data attribute and test afterwards.

// Add class disableonsubmit to your form
    $(document).ready(function () {
        $('form.disableonsubmit').submit(function(e) {
            if ($(this).data('submitted') === true) {
                // Form is already submitted
                console.log('Form is already submitted, waiting response.');
                // Stop form from submitting again
                e.preventDefault();
            } else {
                // Set the data-submitted attribute to true for record
                $(this).data('submitted', true);
            }
        });
    });

Solution 8 - Javascript

Your code actually works on FF, it doesn't work on Chrome.

This works on FF and Chrome.

$(document).ready(function() {
        // Solution for disabling the submit temporarily for all the submit buttons.
        // Avoids double form submit.
        // Doing it directly on the submit click made the form not to submit in Chrome.
        // This works in FF and Chrome.
        $('form').on('submit', function(e){
          //console.log('submit2', e, $(this).find('[clicked=true]'));
          var submit = $(this).find('[clicked=true]')[0];
          if (!submit.hasAttribute('disabled'))
          {
            submit.setAttribute('disabled', true);
            setTimeout(function(){
              submit.removeAttribute('disabled');
            }, 1000);
          }
          submit.removeAttribute('clicked');
          e.preventDefault();
        });
        $('[type=submit]').on('click touchstart', function(){
          this.setAttribute('clicked', true);
        });
      });
    </script>

Solution 9 - Javascript

How to disable submit button

just call a function on onclick event and... return true to submit and false to disable submit. OR call a function on window.onload like :

window.onload = init();

and in init() do something like this :

var theForm = document.getElementById(‘theForm’);
theForm.onsubmit =  // what ever you want to do 

Solution 10 - Javascript

The following worked for me:

var form_enabled = true;
$().ready(function(){
       // allow the user to submit the form only once each time the page loads
       $('#form_id').on('submit', function(){
               if (form_enabled) {
                       form_enabled = false;
                       return true;
               }

               return false;
        });
});

This cancels the submit event if the user tries to submit the form multiple times (by clicking a submit button, pressing Enter, etc.)

Solution 11 - Javascript

I have been using blockUI to avoid browser incompatibilies on disabled or hidden buttons.

http://malsup.com/jquery/block/#element

Then my buttons have a class autobutton:

  $(".autobutton").click(
     function(event) {
        var nv = $(this).html();
        var nv2 = '<span class="fa fa-circle-o-notch fa-spin" aria-hidden="true"></span> ' + nv;
        $(this).html(nv2);
        var form = $(this).parents('form:first');

        $(this).block({ message: null });
        form.submit();                
        });   

Then a form is like that:

<form>
....
<button class="autobutton">Submit</button>   
</form>

Solution 12 - Javascript

Button Code

<button id="submit" name="submit" type="submit" value="Submit">Submit</button>

Disable Button

if(When You Disable the button this Case){
 $(':input[type="submit"]').prop('disabled', true); 
}else{
 $(':input[type="submit"]').prop('disabled', false); 
}

Note: You Case may Be Multiple this time more condition may need

Solution 13 - Javascript

Easy Method:

Javascript & HTML:

$('form#id').submit(function(e){
    $(this).children('input[type=submit]').attr('disabled', 'disabled');
    // this is just for demonstration
    e.preventDefault(); 
    return false;
});

<form id="id">
    <input type="submit"/>
</form>

Note: works perfectly on chrome and edge.

Solution 14 - Javascript

The simplest pure javascript solution is to simply disable the button:

<form id="blah" action="foo.php" method="post" onSubmit="return checkForm();">
  <button id="blahButton">Submit</button>
</form>

document.getElementById('blahButton').disabled = true ;

It works with/without onSubmit. Form stays visible, but nothing can be sumbitted.

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
QuestionmarkzzzView Question on Stackoverflow
Solution 1 - JavascriptJared FarrishView Answer on Stackoverflow
Solution 2 - JavascriptBaigView Answer on Stackoverflow
Solution 3 - JavascriptValamasView Answer on Stackoverflow
Solution 4 - JavascriptShreekar PatelView Answer on Stackoverflow
Solution 5 - JavascriptZiaView Answer on Stackoverflow
Solution 6 - JavascriptsriramView Answer on Stackoverflow
Solution 7 - JavascriptManpreetView Answer on Stackoverflow
Solution 8 - JavascriptPablo PazosView Answer on Stackoverflow
Solution 9 - JavascriptAnkit TyagiView Answer on Stackoverflow
Solution 10 - JavascriptJohn LangfordView Answer on Stackoverflow
Solution 11 - JavascriptMichael ChourdakisView Answer on Stackoverflow
Solution 12 - JavascriptAbhijit PatraView Answer on Stackoverflow
Solution 13 - JavascriptAlitheDevView Answer on Stackoverflow
Solution 14 - JavascriptrolingerView Answer on Stackoverflow