user1027620
user1027620

Reputation: 2785

Target the same control that initiated a jQuery AJAX POST on postback

function pageLoad() {


        $(".VoteUp").live("click", function () {
            var Id = $(this).attr("index");
            d = JSON.stringify({ "Id": Id })
            $.ajax({
                type: 'POST',
                url: '../API/Services.asmx/LikeId',
                data: d,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: testFunc,
                error: errFunc
            });

            $(".VoteUp").each(function () {
                $(this).attr("disabled", "disabled");
            });

        });

        function testFunc(result, userContext, methodName) {
            if (result.d[0] == "true") {
                TheVoteUpControl.val("added");
            }
            else {
                TheVoteUpControl.val("deleted");
            }
            $(".VoteUp").each(function () {
                $(this).removeAttr("disabled");
            });

        }
        function errFunc() {
            alert('error');
        }

How can I change the value of that same .VoteUp control on success? I can't seem to figure out how to target that same control who initiated the event. $(".VoteUp")

Suggestions?

Upvotes: 1

Views: 120

Answers (1)

Rafay
Rafay

Reputation: 31033

the only way i can think of is cache the element in the global variable and catch it in the success callback. also i have moved the enabling and disabling of the $(".VoteUp") in the complete and beforeSend callbacks respectively

    $(".VoteUp").live("click", function (e) { 
        e.preventDefault();
        $this=$(this); <-- cache this in the global variable
        var Id = $(this).attr("index");
        d = JSON.stringify({ "Id": Id })
        $.ajax({
            beforeSend:function(){
              $(".VoteUp").attr("disabled", "disabled");
            },
            type: 'POST',
            url: '../API/Services.asmx/LikeId',
            data: d,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: testFunc,
            error: errFunc
            complete:function(){
                  $(".VoteUp").removeAttr("disabled");
           }
        });
    });


    function testFunc(result, userContext, methodName) {
        if (result.d[0] == "true") {
            $this.val("added");
        }
        else {
            $this.val("deleted");
        }


    }

Upvotes: 1

Related Questions