2

I have a Jquery for click a button to add items to list.here I need to disable the button after click on it and after success it will be enable.here is my Code

$(".addtowishlist").live("click", function (e) {
        var t = $(this);
        $('<img src="/images/loading.gif" alt="loading" id="ajax-loader-wish"/>').insertAfter(t);
        $("#simplemodal-overlay").unbind("click");
        var n = t.parent().find("select[name='code']").val();

        var i = t.parent().find("input:hidden[name='account']").val();
        var s = t.parent().find("input:hidden[name='itemname']").val();
        var o = t.parent().find("input:hidden[name='itemnumber']").val();
        var u = (new URI).addQuery("code", n).addQuery("qty", 0).addQuery("addtowishlist", true).addQuery("account", i).addQuery("itemname", s).addQuery("itemnumber", o);
        $.ajax({
            url: u,
            type: "GET",
            success: function (e) {
                t.closest(".innerwishlist").hide();
                $(".wishlistThankYou").show()
            }
        });
        return false
    });

I tried

$(".addtowishlist").attr('disabled', 'disabled'); 
and code for enable 

 $(".addtowishlist").attr('disabled', ''); 

its works and disable the button but it doesn't enable the button after success. anybody help ?

Arun
  • 1,402
  • 10
  • 32
  • 59
  • 2
    Unrelated: `.live()` is deprecated, and removed in jQuery 1.9. Convert to `.on()`. – Barmar May 20 '13 at 06:36
  • possible duplicate of [jQuery to disable button not work](http://stackoverflow.com/questions/8707423/jquery-to-disable-button-not-work) – Barmar May 20 '13 at 06:37
  • +1 Agree with @Barmar that `.live()` is no longer supported. – Smile May 20 '13 at 06:38

5 Answers5

6

It is recommended to use .prop instead:

$(".addtowishlist").prop('disabled', true); 
$(".addtowishlist").prop('disabled', false); 

Or if you use old version of jQuery, use $(".addtowishlist").removeAttr("disabled"); to remove the attribute.

xdazz
  • 158,678
  • 38
  • 247
  • 274
4
$(".addtowishlist").attr('disabled', true); 
and code for enable 

 $(".addtowishlist").attr('disabled', false); 
Vond Ritz
  • 2,022
  • 14
  • 15
1

try this:

$(".addtowishlist").removeAttr("disabled");
Larry McKenzie
  • 3,253
  • 25
  • 22
0

If the button is itself a jQuery styled button (with .button()) you will need to refresh the state of the button so that the correct classes are added / removed.

$( ".selector" ).button( "refresh" );
Tom Chamberlain
  • 2,955
  • 20
  • 24
0

If you are using jQuery version 1.6+, then

$(".addtowishlist").prop( "disabled", true);

is recommended to disable the field. And for enabling use

$(".addtowishlist").prop( "disabled", false );

For older jQuery versions use below for disabling

$(".addtowishlist").attr('disabled', 'disabled');

and below one for enabling.

$(".addtowishlist").removeAttr('disabled');
Don D
  • 726
  • 1
  • 9
  • 19