Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I temporarily disable a submit button for 3 seconds (onsubmit), then re-enable it?

I am fairly new to using javascript and here is what I have so far:

<!-- this is to disable the submit button and then re-enable it after 3 sec -->

<script type="text/javascript">

    function enable()
    {
    var x = document.LAYOUTFORM.getElementById("create_button");
    setTimeout(x.removeAttribute("disabled"), 3000);
    }

</script>

And for the button I have this:

<INPUT TYPE="SUBMIT" VALUE=" Create PDF " class="FORMBUTTON" ID="create_button" onclick="javascript:this.disabled=true;javascript:enable();">

I have messed with this for hours and most of you will look at it and know what is wrong immediately. My form ID and name is LAYOUTFORM. Can anyone tell me what I am doing wrong here?

For bonus points I would also like the text of the button to temporarily change to "Creating..." while it is disabled, and then back to Create PDF again.

like image 241
AmazingChase Avatar asked May 15 '12 01:05

AmazingChase


People also ask

How do I make my submit button disabled?

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.

How do I enable a submit button?

Approach: To enable or disable the form submit button, we use the below code snippet. $('#enabled'). click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button'). removeAttr('disabled'); } else { $('#submit-button').

How disable submit button after click in MVC?

Attach a javascript method to the button click to first disable the button, and then submit the form. This way if the user double clicks the button, the second click will be disabled and the form will only be submitted once.


1 Answers

Simplest way:

<input ... onclick="lockoutSubmit(this)">

In your javascript:

function lockoutSubmit(button) {
    var oldValue = button.value;

    button.setAttribute('disabled', true);
    button.value = '...processing...';

    setTimeout(function(){
        button.value = oldValue;
        button.removeAttribute('disabled');
    }, 3000)
}
like image 185
ninjagecko Avatar answered Oct 02 '22 07:10

ninjagecko