0

I am trying to disable a button after it has been clicked so that it cannot be double clicked.

This is what I have:

<input id="btn" type="button" disabled="disabled" value="Log On" onclick="logonDisableSubmitButtons()"/>

And my javascript:

function logonDisableSubmitButtons(clickedButton) {

    $("input[type='button']").each(function() {
      if (this.name != clickedButton)
        $(this).attr("disabled", "disabled");
      else {
        //hiding the actually clicked button
        $(this).hide();
        //Creating dummy button to same like clicked button
        $(this).after('<input type="button" disabled="disabled" value="' + $(this).val() + '" class="' + $(this).attr('class') + '" />');
      }
    });

  }

The button disables but it doesn't submit the form.

How do I get it to disable after click and then submit the form?

3 Answers 3

1

Use a submit input instead of a button input:

<input id="btn" type="submit" disabled="disabled" value="Log On" onclick="logonDisableSubmitButtons()"/>
Sign up to request clarification or add additional context in comments.

1 Comment

Easy enough! Worked perfectly!
0

Manually submit the form with the submit() method in the same block where you are disabling the button.

Comments

0

You would have a much easier time managing this by removing the onclick attribute from your input and simply binding the click event on $(document).ready();, like this:

    <form id="myForm">
        <input type="submit" id="myInput" value="click me"/> <br />
        <input type="button" class="someClass" value="random button 1" />
        <input type="button" class="someClass" value="random button 2" />
        <input type="button" class="someClass" value="random button 3" />
        <input type="button" class="someClass" value="random button 4" />
    </form>

<script type="text/javascript">
$(document).ready(function(){
    $('#myInput').click(function(e){  
        e.preventDefault(); //<--this prevents the form from being submitted on click.        
        logonDisableSubmitButtons(this);
    });
});
function logonDisableSubmitButtons(clickedButton) {
    $(clickedButton).attr("disabled", "disabled");
    $("input[type='button']").each(function(i, input) {
            $(input).hide();
            $(this).after(
                '<input type="button" disabled="disabled" value="' + 
                $(this).val() + " from logonDisableSubmitButtons() method" +
                '" class="' +
                $(this).attr('class') + '" />'
            );
    });
    $("#myForm").submit(); //<--this will submit the form
}​
    </script>

Here's a working example on jsFiddle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.