1
<input class="submit2" type="button" name="submit2"  value="Submit" id="submit1" onclick="">

I want to disable the button on my page after clicking it. to avoid double click.

i have tried following code

$(document).ready(function() {

    $('.submit2').click(function() {

        $("input.submit2").attr('disabled', true);

    });
});​

but it is working only when the input type of button is submit not button.

how to resolve this issue.

Thanks in advance.

4 Answers 4

2

You don't need to select twice what you already have selected:

$(document).ready(function(){
    $('.submit2').click(function() { 
        $(this).attr('disabled', true);
    }); 
});

$(this) in this case refers to the button you just clicked

Sign up to request clarification or add additional context in comments.

Comments

1

also,inorder to make it compatible with all browser, the value of disabled attr should be "disabled"

use it like

$("input.submit2").attr('disabled',"disabled");

The above solution is perfect if you are using jquery version 1.5 and earlier and if you are using jQuery 1.6+ then use prop to set attribute

$("input.submit2").prop('disabled',true); // here jQuery will normalize the value and set the property

4 Comments

disabled variable is undefined
jQuery normalizes boolean attributes and allows true/false to be passed afaik.
@zerkms: it should be string so changed it.
@sg3s: JQuery version 1.5 and earlier, the attr('disabled',true) was not setting up disabled properly across browsers. so we need to set value as "disabled" not true. here is one similar post link
0

It's an old school HTML relic. The attribute value is 'disabled'='disabled':

$("input.submit2").attr('disabled','disabled');

Comments

0

You can do it now:

$(document).ready(function(){
     var $submitButton = $(this, "input[type='submit']");
     $submitButton.attr("disabled", "true");
});

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.