jQuery – How to disabled submit button after clicked

Often times, users like to press a few times on the submit button to make sure the button is surely clicked, and causing the double form submission issue. The common solution is disables the submit button after user clicked on it.

1. Enable / Disable submit button

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button.


$("#btnSubmit").attr("disabled", true);

1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.


$('#btnSubmit').attr("disabled", false);	
or
$('#btnSubmit').removeAttr("disabled");

2. jQuery full example


<!DOCTYPE html>
<html lang="en">

<body>
<h1>jQuery - How to disabled submit button after clicked</h1>

<form id="formABC" action="#" method="POST">
    <input type="submit" id="btnSubmit" value="Submit"></input>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<input type="button" value="i am normal abc" id="btnTest"></input>

<script>
    $(document).ready(function () {

        $("#formABC").submit(function (e) {

            //stop submitting the form to see the disabled button effect
            e.preventDefault();

            //disable the submit button
            $("#btnSubmit").attr("disabled", true);

            //disable a normal button
            $("#btnTest").attr("disabled", true);

            return true;

        });
    });
</script>

</body>
</html>

References

  1. jQuery Submit API

25 comments on “jQuery – How to disabled submit button after clicked

  1. Thanks for this article. I’d also like to convey that it can always be hard if you find yourself in school and starting out to initiate a long history of credit. There are many college students who are only trying to live and have long or good credit history can often be a difficult thing to have. edgcecaeedcfbekk

  2. Thanks for this awesome post, but there is just one issue that if there are HTML5 required fields and if form is submitted without them, submit button is still disabled. Please help me to cope with it.

  3. I personally think the best way to achieve this is to “redirect” to another page, saying “submission confirmed”. Since, if the user refreshes the page or clicks the F5 button, the submission will occur again, if you are talking about database submission anyway.

  4. hi can you help me figuring how to disable after 5 clicks of the submit button and how to disable submitting the form with the enter keyboard?

    best regards

  5. how did you solve problem with page caching

    example: open page (browser cache it)
    type submit button, that disable next submit

    on followed page is found error in datas and showed error
    that user may use back button to correct form

    but how… submit disabled form???

Leave a Comment

Your email address will not be published. Required fields are marked *