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.

sg3s
  • 9,105
  • 3
  • 32
  • 52
vinod_raje
  • 11
  • 1
  • 2

4 Answers4

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

zerkms
  • 230,357
  • 57
  • 408
  • 498
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
Naren Sisodiya
  • 6,846
  • 2
  • 22
  • 35
  • `disabled` variable is undefined – zerkms Apr 21 '12 at 05:29
  • jQuery normalizes boolean attributes and allows true/false to be passed afaik. – sg3s Apr 21 '12 at 05:29
  • @zerkms: it should be string so changed it. – Naren Sisodiya Apr 21 '12 at 05:31
  • 1
    @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](http://stackoverflow.com/questions/1414365/how-to-disable-an-input-with-jquery) – Naren Sisodiya Apr 21 '12 at 05:43
0

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

$("input.submit2").attr('disabled','disabled');
McGarnagle
  • 96,448
  • 30
  • 213
  • 255
Sujith Kumar KS
  • 1,031
  • 12
  • 23
0

You can do it now:

$(document).ready(function(){
     var $submitButton = $(this, "input[type='submit']");
     $submitButton.attr("disabled", "true");
});
Juan Cortés
  • 18,689
  • 8
  • 63
  • 88
Gopal
  • 227
  • 4
  • 16