[jQuery] variable scope in .post
Hi, I have some problems setting a global variable from within the success() function inside a .post. Here is a simplified version of what I have: function addedit() { var lk_id=0; $.ajax({url:'ajax/my_script.php', type:'POST', dataType:'html', data:'optype=add', success:function(data) { if (data!=null data!='') { //alert(lk_id); lk_id=parseInt(data); } } }); alert(lk_id); } My script returns a single number and I want lk_id to get that value in the success() function of the ajax call. The commented alert() in the success function can read the value of the outer lk_id (in this case 0), lk_id is set to the new value but the last alert() of the function returns 0 again. This function is called from a link with the onclick=addedit() attribute. I am using firefox 2 I suppose I could use the load() function to put the result in a hidden input, then read that input but it seems a little too complicated. Any help regarding this problem is appreciated -- Dan ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] variable scope in .post
Dan Caragea schrieb: Hi, I have some problems setting a global variable from within the success() function inside a .post. Here is a simplified version of what I have: function addedit() { var lk_id=0; $.ajax({url:'ajax/my_script.php', type:'POST', dataType:'html', data:'optype=add', success:function(data) { if (data!=null data!='') { //alert(lk_id); lk_id=parseInt(data); } } }); alert(lk_id); } The problem is not the scope. $.ajax is asynchronous and therefore the methods returns, calls the alters and then some (undefined) time later the success callback is executed. While it is possible to use $.ajax for synchronous calls, I (and I guess everyone else on this list) recommend to work with the callbacks, putting your altert inside the success callback or calling another function from there. -- Jörn Zaefferer http://bassistance.de ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] variable scope in .post
your code is close, but because ajax is ASYNCHRONOUS the var gets set after the alert is executed. normally you would do everything that deals with the lk_id inside the success function (or a function called from inside the success function) On 1/15/07, Dan Caragea [EMAIL PROTECTED] wrote: Hi, I have some problems setting a global variable from within the success() function inside a .post. Here is a simplified version of what I have: function addedit() { var lk_id=0; $.ajax({url:'ajax/my_script.php', type:'POST', dataType:'html', data:'optype=add', success:function(data) { if (data!=null data!='') { //alert(lk_id); lk_id=parseInt(data); } } }); alert(lk_id); } My script returns a single number and I want lk_id to get that value in the success() function of the ajax call. The commented alert() in the success function can read the value of the outer lk_id (in this case 0), lk_id is set to the new value but the last alert() of the function returns 0 again. This function is called from a link with the onclick=addedit() attribute. I am using firefox 2 I suppose I could use the load() function to put the result in a hidden input, then read that input but it seems a little too complicated. Any help regarding this problem is appreciated -- Dan ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] variable scope in .post
Man, that was easy! Thanks for help, it works like a charm now. Dan Ⓙⓐⓚⓔ wrote: your code is close, but because ajax is ASYNCHRONOUS the var gets set after the alert is executed. normally you would do everything that deals with the lk_id inside the success function (or a function called from inside the success function) On 1/15/07, Dan Caragea [EMAIL PROTECTED] wrote: Hi, I have some problems setting a global variable from within the success() function inside a .post. Here is a simplified version of what I have: function addedit() { var lk_id=0; $.ajax({url:'ajax/my_script.php', type:'POST', dataType:'html', data:'optype=add', success:function(data) { if (data!=null data!='') { //alert(lk_id); lk_id=parseInt(data); } } }); alert(lk_id); } My script returns a single number and I want lk_id to get that value in the success() function of the ajax call. The commented alert() in the success function can read the value of the outer lk_id (in this case 0), lk_id is set to the new value but the last alert() of the function returns 0 again. This function is called from a link with the onclick=addedit() attribute. I am using firefox 2 I suppose I could use the load() function to put the result in a hidden input, then read that input but it seems a little too complicated. Any help regarding this problem is appreciated -- Dan ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
[jQuery] jquery variable scope in $.post (this)
Hi All,I am using a $.post to enter some data in the db, and using the callback function to set some html on the screen based on the html that the post returned. I have the following code:$(div.input form).submit(function() { /*get form vars here to form params_hash*/ $.post(url,{params_hash_here}, function(html) { var new_var = parsing_the_response_html_here_correctly; $([EMAIL PROTECTED],this).html(new_var); });});However, the second piece of code that alters the screen html applies to the whole screen (effectively ignoring 'this' context), and not to the 'this' context that the post was part of. (Basically there are multiple submit buttons on the page and I want to post data from only one of them - the one who's submit button I clicked.) I also tried the following modification, thinking that I could get around the problem with variable scoping$(div.input form).submit(function() { /*get form vars here to form params_hash*/ /*now I am initializing new_var before the post*/ var new_var = ''; $.post(url,{params_hash_here}, function(html) { new_var = parsing_the_response_html_here_correctly; }); $([EMAIL PROTECTED],this).html(new_var); });So I define new_var before the post, use the callback function to set its value, and then use the html changing element in the code block that I know 'this' works for. This solution did not work at all and caused some weird hangups. Basically this boils down to my misunderstanding of variable scope, in particular the scope of 'this' in jquery. Any help is much appreciated!!!Julius-- - http://openwetware.org/wiki/User:Lucks- ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] jquery variable scope in $.post (this)
You could try caching the this param at the top of the submit callback (var self = this). $.post is not a regular jQuery fn, so it's doesn't convert this into anything (so this is still the function object). Bottom line: do self = this at top of the submit function, then use self for context.-- YehudaOn 10/28/06, Julius Lucks [EMAIL PROTECTED] wrote:Hi All,I am using a $.post to enter some data in the db, and using the callback function to set some html on the screen based on the html that the post returned. I have the following code: $(div.input form).submit(function() { /*get form vars here to form params_hash*/ $.post(url,{params_hash_here}, function(html) { var new_var = parsing_the_response_html_here_correctly; $([EMAIL PROTECTED],this).html(new_var); });});However, the second piece of code that alters the screen html applies to the whole screen (effectively ignoring 'this' context), and not to the 'this' context that the post was part of. (Basically there are multiple submit buttons on the page and I want to post data from only one of them - the one who's submit button I clicked.) I also tried the following modification, thinking that I could get around the problem with variable scoping$(div.input form).submit(function() { /*get form vars here to form params_hash*/ /*now I am initializing new_var before the post*/ var new_var = ''; $.post(url,{params_hash_here}, function(html) { new_var = parsing_the_response_html_here_correctly; }); $([EMAIL PROTECTED],this).html(new_var); });So I define new_var before the post, use the callback function to set its value, and then use the html changing element in the code block that I know 'this' works for. This solution did not work at all and caused some weird hangups. Basically this boils down to my misunderstanding of variable scope, in particular the scope of 'this' in jquery. Any help is much appreciated!!!Julius-- - http://openwetware.org/wiki/User:Lucks- ___jQuery mailing listdiscuss@jquery.com http://jquery.com/discuss/-- Yehuda KatzWeb Developer | Wycats Designs(ph)718.877.1325 ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] jquery variable scope in $.post (this)
Fantastic!JuliusOn 10/28/06, Yehuda Katz [EMAIL PROTECTED] wrote: You could try caching the this param at the top of the submit callback (var self = this). $.post is not a regular jQuery fn, so it's doesn't convert this into anything (so this is still the function object). Bottom line: do self = this at top of the submit function, then use self for context.-- YehudaOn 10/28/06, Julius Lucks [EMAIL PROTECTED] wrote: Hi All,I am using a $.post to enter some data in the db, and using the callback function to set some html on the screen based on the html that the post returned. I have the following code: $(div.input form).submit(function() { /*get form vars here to form params_hash*/ $.post(url,{params_hash_here}, function(html) { var new_var = parsing_the_response_html_here_correctly; $([EMAIL PROTECTED],this).html(new_var); });});However, the second piece of code that alters the screen html applies to the whole screen (effectively ignoring 'this' context), and not to the 'this' context that the post was part of. (Basically there are multiple submit buttons on the page and I want to post data from only one of them - the one who's submit button I clicked.) I also tried the following modification, thinking that I could get around the problem with variable scoping$(div.input form).submit(function() { /*get form vars here to form params_hash*/ /*now I am initializing new_var before the post*/ var new_var = ''; $.post(url,{params_hash_here}, function(html) { new_var = parsing_the_response_html_here_correctly; }); $([EMAIL PROTECTED],this).html(new_var); });So I define new_var before the post, use the callback function to set its value, and then use the html changing element in the code block that I know 'this' works for. This solution did not work at all and caused some weird hangups. Basically this boils down to my misunderstanding of variable scope, in particular the scope of 'this' in jquery. Any help is much appreciated!!!Julius-- - http://openwetware.org/wiki/User:Lucks- ___jQuery mailing listdiscuss@jquery.com http://jquery.com/discuss/-- Yehuda KatzWeb Developer | Wycats Designs(ph)718.877.1325 ___jQuery mailing listdiscuss@jquery.com http://jquery.com/discuss/-- -http://openwetware.org/wiki/User:Lucks - ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/