Jquery AJAX: How to Change button value on “success”?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

I think this is not correct, because the callback is run in the global scope.

I think this is not correct, because the callback is run in the global scope. Untested, but try just before $. Ajax to write var $this = $(this) and then in your callback use $this.

Val(json. Update) Edit: Updated code snippet to ensure local $this by declaring with var. Other posts suggest using var button = $(this) which is probably better in bigger projects where keeping track of the variables is more challenging - but all the answers are the same really.

Worked like a charm! Thanks Billy! – Aakash Goel Apr 22 at 20:36 @Aakash, This will work only assuming it is not possible to click another button before the ajax call returns.

If you do, then $this variable will be set to the 2nd button at the time the ajax call of the first returns. – James Montagne Apr 22 at 20:41 @kingjiv: how do I do that? – Aakash Goel Apr 22 at 20:47 @kingjiv: not true; provided $this is locally scoped, not a global (using var $this).

– Martijn Pieters Apr 22 at 21:54 Guess I misread that, for some reason I was thinking the suggestion was a global variable. – James Montagne Apr 22 at 23:19.

The problem is that 'this', inside the ajax request, points to the xhr object, not the button. You should store the reference to the button prior to do the call, like var button = $(this); and later updating it button. Val(json.

Update).

Store the button in a local variable in the outer loop, then refer to that variable in the inner scope of the success handler: $(document). Ready(function(){ $("inputtype='button'"). Click(function(){ var $btn = $(this) var selected = $btn.

Attr("id"); var val = prompt("Enter new value",0); $. Ajax({ url:'updateCostItems. Php', data:{toupdate:selected, updatewith:val}, type:'GET', dataType:'json', success:function(json){ $btn.

Val(json. Update); }, error:function(xhr, status){ alert("Problem"); }, }); }); }).

$(this) will not refer to the button at the time the success function is called. You will need to pass the button (or the button's id) along as a parameter to your callback. Storing a single global variable is not sufficient since you could potentially click on a 2nd button before the first ajax call returns.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions