feeling
feeling

Reputation: 25

I want to change button to disabled

I want to change attribute button to disabled after I get data from AJAX but it is disabled all my button but I want to disabled only the button I click how can I do ? Please help thank.

$(".wishbutton").click(function(){
    var prodid= $(this).parent().parent().find(".hideprodid").val();
    $.post("secondata/mydata.php",{action:'addlist',proid:prodid},function(data){
        var response = JSON.parse(data);
        if (response[0].response=="login" ){
            alert("Please Log In First!");
            }
        else if (response[0].response=="success"){
            $(".wishbutton").attr("disabled","disabled");
            alert("add to wish list");

            }

        });

    });

Upvotes: 1

Views: 54

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

this inside the event handler will refer to the clicked button, but you can't use that inside the ajax handler because it is executed with a different context. So you can use a closure variable

$(".wishbutton").click(function () {
    var $this = $(this);
    var prodid = $this.parent().parent().find(".hideprodid").val();
    $.post("secondata/mydata.php", {
        action: 'addlist',
        proid: prodid
    }, function (data) {
        var response = JSON.parse(data);
        if (response[0].response == "login") {
            alert("Please Log In First!");
        } else if (response[0].response == "success") {
            $this.prop("disabled", true);
            alert("add to wish list");
        }
    });
});

Upvotes: 3

Related Questions