[jQuery] Re: AJAX Success Callback referring to $(this)
I guess I'm not understanding what you're trying to do. The code I provided is the standard way of referencing "this" when you want to reference the original context in an inner function. -- Josh - Original Message - From: "Wayne" <[EMAIL PROTECTED]> To: "jQuery (English)" Sent: Wednesday, October 08, 2008 2:18 PM Subject: [jQuery] Re: AJAX Success Callback referring to $(this) Right, but that trigger doesn't work in the context of the anonymous function. Unless, I'm missing something you changed. -Wayne On Oct 8, 3:43 pm, "Josh Nathanson" <[EMAIL PROTECTED]> wrote: Ok, I think I see what's happening -- you have something like this: $("whatever").click(function() { $.ajax({ blah blah.. So you just need to do this: $("whatever").click(function() { var trigger = this; $.ajax({ success: function() { // do whatever with 'trigger' } }); }); -- Josh - Original Message - From: "Wayne" <[EMAIL PROTECTED]> To: "jQuery (English)" Sent: Wednesday, October 08, 2008 12:15 PM Subject: [jQuery] Re: AJAX Success Callback referring to $(this) OK. I haven't tried this yet, but how is this any different? Even though you're moving the ajax call inside of another function, aren't you abstracting the same logic out by calling the anonymous function inside of success? Maybe I'm not understanding what "trigger" and "trigger condition" are supposed to represent, but I assumed that was related to my isNaN(r) logic. Thanks, -Wayne On Oct 8, 12:36 pm, "Josh Nathanson" <[EMAIL PROTECTED]> wrote: > "this" referenced in the success callback will refer to the jQuery > object > when you do an ajax call. > You might want to put the ajax call within another function that can > also > receive information about the triggering element: > doAjax: function( trigger ) { > jQuery.ajax({ > // etc. > success: function() { > if ( trigger condition ) { > // do stuff > } > } > }); > } > $("triggeringelement").click(function() { > doAjax( this ); > }); > -- Josh > - Original Message - > From: "Wayne" <[EMAIL PROTECTED]> > To: "jQuery (English)" > Sent: Wednesday, October 08, 2008 7:49 AM > Subject: [jQuery] AJAX Success Callback referring to $(this) > > I've been looking for this quite a bit, today, and haven't quite found > > what I'm looking for, so maybe someone can point me in the right > > direction. > > I'm performing an AJAX request, and I pass an anonymous function as > > the success callback, upon which I will want to do some DOM > > modification to the element that triggered the AJAX event. Ordinarily, > > $(this) would work fine for doing this, but it seems that $(this) > > points instead to something besides a DOM element. > > First, what does $(this) point to when you're inside the AJAX Success > > callback. > > Second, what is considered the best practice for accessing the > > triggering element after a successful AJAX query? > > Any help is appreciated, > > Wayne
[jQuery] Re: AJAX Success Callback referring to $(this)
There's no reason why Josh's code wouldn't work (closure = good!) I suggest just trying it :-) On Oct 8, 5:18 pm, Wayne <[EMAIL PROTECTED]> wrote: > Right, but that trigger doesn't work in the context of the anonymous > function. Unless, I'm missing something you changed. > > -Wayne > > On Oct 8, 3:43 pm, "Josh Nathanson" <[EMAIL PROTECTED]> wrote: > > > Ok, I think I see what's happening -- you have something like this: > > > $("whatever").click(function() { > > $.ajax({ blah blah.. > > > So you just need to do this: > > > $("whatever").click(function() { > > var trigger = this; > > $.ajax({ > > success: function() { > > // do whatever with 'trigger' > > } > > }); > > > }); > > > -- Josh > > > - Original Message - > > From: "Wayne" <[EMAIL PROTECTED]> > > To: "jQuery (English)" > > Sent: Wednesday, October 08, 2008 12:15 PM > > Subject: [jQuery] Re: AJAX Success Callback referring to $(this) > > > OK. I haven't tried this yet, but how is this any different? Even > > though you're moving the ajax call inside of another function, aren't > > you abstracting the same logic out by calling the anonymous function > > inside of success? Maybe I'm not understanding what "trigger" and > > "trigger condition" are supposed to represent, but I assumed that was > > related to my isNaN(r) logic. > > > Thanks, > > -Wayne > > > On Oct 8, 12:36 pm, "Josh Nathanson" <[EMAIL PROTECTED]> wrote: > > > "this" referenced in the success callback will refer to the jQuery object > > > when you do an ajax call. > > > > You might want to put the ajax call within another function that can also > > > receive information about the triggering element: > > > > doAjax: function( trigger ) { > > > jQuery.ajax({ > > > // etc. > > > success: function() { > > > if ( trigger condition ) { > > > // do stuff > > > } > > > } > > > }); > > > > } > > > > $("triggeringelement").click(function() { > > > doAjax( this ); > > > > }); > > > > -- Josh > > > > - Original Message - > > > From: "Wayne" <[EMAIL PROTECTED]> > > > To: "jQuery (English)" > > > Sent: Wednesday, October 08, 2008 7:49 AM > > > Subject: [jQuery] AJAX Success Callback referring to $(this) > > > > > I've been looking for this quite a bit, today, and haven't quite found > > > > what I'm looking for, so maybe someone can point me in the right > > > > direction. > > > > > I'm performing an AJAX request, and I pass an anonymous function as > > > > the success callback, upon which I will want to do some DOM > > > > modification to the element that triggered the AJAX event. Ordinarily, > > > > $(this) would work fine for doing this, but it seems that $(this) > > > > points instead to something besides a DOM element. > > > > > First, what does $(this) point to when you're inside the AJAX Success > > > > callback. > > > > > Second, what is considered the best practice for accessing the > > > > triggering element after a successful AJAX query? > > > > > Any help is appreciated, > > > > Wayne
[jQuery] Re: AJAX Success Callback referring to $(this)
Right, but that trigger doesn't work in the context of the anonymous function. Unless, I'm missing something you changed. -Wayne On Oct 8, 3:43 pm, "Josh Nathanson" <[EMAIL PROTECTED]> wrote: > Ok, I think I see what's happening -- you have something like this: > > $("whatever").click(function() { > $.ajax({ blah blah.. > > So you just need to do this: > > $("whatever").click(function() { > var trigger = this; > $.ajax({ > success: function() { > // do whatever with 'trigger' > } > }); > > }); > > -- Josh > > - Original Message - > From: "Wayne" <[EMAIL PROTECTED]> > To: "jQuery (English)" > Sent: Wednesday, October 08, 2008 12:15 PM > Subject: [jQuery] Re: AJAX Success Callback referring to $(this) > > OK. I haven't tried this yet, but how is this any different? Even > though you're moving the ajax call inside of another function, aren't > you abstracting the same logic out by calling the anonymous function > inside of success? Maybe I'm not understanding what "trigger" and > "trigger condition" are supposed to represent, but I assumed that was > related to my isNaN(r) logic. > > Thanks, > -Wayne > > On Oct 8, 12:36 pm, "Josh Nathanson" <[EMAIL PROTECTED]> wrote: > > "this" referenced in the success callback will refer to the jQuery object > > when you do an ajax call. > > > You might want to put the ajax call within another function that can also > > receive information about the triggering element: > > > doAjax: function( trigger ) { > > jQuery.ajax({ > > // etc. > > success: function() { > > if ( trigger condition ) { > > // do stuff > > } > > } > > }); > > > } > > > $("triggeringelement").click(function() { > > doAjax( this ); > > > }); > > > -- Josh > > > - Original Message - > > From: "Wayne" <[EMAIL PROTECTED]> > > To: "jQuery (English)" > > Sent: Wednesday, October 08, 2008 7:49 AM > > Subject: [jQuery] AJAX Success Callback referring to $(this) > > > > I've been looking for this quite a bit, today, and haven't quite found > > > what I'm looking for, so maybe someone can point me in the right > > > direction. > > > > I'm performing an AJAX request, and I pass an anonymous function as > > > the success callback, upon which I will want to do some DOM > > > modification to the element that triggered the AJAX event. Ordinarily, > > > $(this) would work fine for doing this, but it seems that $(this) > > > points instead to something besides a DOM element. > > > > First, what does $(this) point to when you're inside the AJAX Success > > > callback. > > > > Second, what is considered the best practice for accessing the > > > triggering element after a successful AJAX query? > > > > Any help is appreciated, > > > Wayne > >
[jQuery] Re: AJAX Success Callback referring to $(this)
Ok, I think I see what's happening -- you have something like this: $("whatever").click(function() { $.ajax({ blah blah.. So you just need to do this: $("whatever").click(function() { var trigger = this; $.ajax({ success: function() { // do whatever with 'trigger' } }); }); -- Josh - Original Message - From: "Wayne" <[EMAIL PROTECTED]> To: "jQuery (English)" Sent: Wednesday, October 08, 2008 12:15 PM Subject: [jQuery] Re: AJAX Success Callback referring to $(this) OK. I haven't tried this yet, but how is this any different? Even though you're moving the ajax call inside of another function, aren't you abstracting the same logic out by calling the anonymous function inside of success? Maybe I'm not understanding what "trigger" and "trigger condition" are supposed to represent, but I assumed that was related to my isNaN(r) logic. Thanks, -Wayne On Oct 8, 12:36 pm, "Josh Nathanson" <[EMAIL PROTECTED]> wrote: "this" referenced in the success callback will refer to the jQuery object when you do an ajax call. You might want to put the ajax call within another function that can also receive information about the triggering element: doAjax: function( trigger ) { jQuery.ajax({ // etc. success: function() { if ( trigger condition ) { // do stuff } } }); } $("triggeringelement").click(function() { doAjax( this ); }); -- Josh - Original Message - From: "Wayne" <[EMAIL PROTECTED]> To: "jQuery (English)" Sent: Wednesday, October 08, 2008 7:49 AM Subject: [jQuery] AJAX Success Callback referring to $(this) > I've been looking for this quite a bit, today, and haven't quite found > what I'm looking for, so maybe someone can point me in the right > direction. > I'm performing an AJAX request, and I pass an anonymous function as > the success callback, upon which I will want to do some DOM > modification to the element that triggered the AJAX event. Ordinarily, > $(this) would work fine for doing this, but it seems that $(this) > points instead to something besides a DOM element. > First, what does $(this) point to when you're inside the AJAX Success > callback. > Second, what is considered the best practice for accessing the > triggering element after a successful AJAX query? > Any help is appreciated, > Wayne
[jQuery] Re: AJAX Success Callback referring to $(this)
OK. I haven't tried this yet, but how is this any different? Even though you're moving the ajax call inside of another function, aren't you abstracting the same logic out by calling the anonymous function inside of success? Maybe I'm not understanding what "trigger" and "trigger condition" are supposed to represent, but I assumed that was related to my isNaN(r) logic. Thanks, -Wayne On Oct 8, 12:36 pm, "Josh Nathanson" <[EMAIL PROTECTED]> wrote: > "this" referenced in the success callback will refer to the jQuery object > when you do an ajax call. > > You might want to put the ajax call within another function that can also > receive information about the triggering element: > > doAjax: function( trigger ) { > jQuery.ajax({ > // etc. > success: function() { > if ( trigger condition ) { > // do stuff > } > } > }); > > } > > $("triggeringelement").click(function() { > doAjax( this ); > > }); > > -- Josh > > - Original Message - > From: "Wayne" <[EMAIL PROTECTED]> > To: "jQuery (English)" > Sent: Wednesday, October 08, 2008 7:49 AM > Subject: [jQuery] AJAX Success Callback referring to $(this) > > > I've been looking for this quite a bit, today, and haven't quite found > > what I'm looking for, so maybe someone can point me in the right > > direction. > > > I'm performing an AJAX request, and I pass an anonymous function as > > the success callback, upon which I will want to do some DOM > > modification to the element that triggered the AJAX event. Ordinarily, > > $(this) would work fine for doing this, but it seems that $(this) > > points instead to something besides a DOM element. > > > First, what does $(this) point to when you're inside the AJAX Success > > callback. > > > Second, what is considered the best practice for accessing the > > triggering element after a successful AJAX query? > > > Any help is appreciated, > > Wayne > >
[jQuery] Re: AJAX Success Callback referring to $(this)
"this" referenced in the success callback will refer to the jQuery object when you do an ajax call. You might want to put the ajax call within another function that can also receive information about the triggering element: doAjax: function( trigger ) { jQuery.ajax({ // etc. success: function() { if ( trigger condition ) { // do stuff } } }); } $("triggeringelement").click(function() { doAjax( this ); }); -- Josh - Original Message - From: "Wayne" <[EMAIL PROTECTED]> To: "jQuery (English)" Sent: Wednesday, October 08, 2008 7:49 AM Subject: [jQuery] AJAX Success Callback referring to $(this) I've been looking for this quite a bit, today, and haven't quite found what I'm looking for, so maybe someone can point me in the right direction. I'm performing an AJAX request, and I pass an anonymous function as the success callback, upon which I will want to do some DOM modification to the element that triggered the AJAX event. Ordinarily, $(this) would work fine for doing this, but it seems that $(this) points instead to something besides a DOM element. First, what does $(this) point to when you're inside the AJAX Success callback. Second, what is considered the best practice for accessing the triggering element after a successful AJAX query? Any help is appreciated, Wayne
[jQuery] Re: AJAX Success Callback referring to $(this)
Here's a context of when it would be useful: jQuery.ajax({ data: inputs.join('&'), url: "delete_bline.php", timeout: 5000, error: function() { alert("Sorry, deleting the baseline could not be completed at this time. Please try again, later."); }, success: function(r) { delete_failed = ( isNaN(r) ); if( !delete_failed ) { var original_item = $(this).parents("li"); // hide this element original_item.hide("slide", { direction: "up" }); // remove the matching node in the remeasures list $( "#ri" + original_item.attr("id").substr(2) ).remove(); // remove this node original_item.remove(); } Thanks, -Wayne On Oct 8, 10:57 am, MorningZ <[EMAIL PROTECTED]> wrote: > How about posting what you have? that would give a good basis to > start to explain it > > To hope to help though, since the Ajax request is made asynchronously, > it loses the context of "this" > > On Oct 8, 10:49 am, Wayne <[EMAIL PROTECTED]> wrote: > > > I've been looking for this quite a bit, today, and haven't quite found > > what I'm looking for, so maybe someone can point me in the right > > direction. > > > I'm performing an AJAX request, and I pass an anonymous function as > > the success callback, upon which I will want to do some DOM > > modification to the element that triggered the AJAX event. Ordinarily, > > $(this) would work fine for doing this, but it seems that $(this) > > points instead to something besides a DOM element. > > > First, what does $(this) point to when you're inside the AJAX Success > > callback. > > > Second, what is considered the best practice for accessing the > > triggering element after a successful AJAX query? > > > Any help is appreciated, > > Wayne > >
[jQuery] Re: AJAX Success Callback referring to $(this)
How about posting what you have? that would give a good basis to start to explain it To hope to help though, since the Ajax request is made asynchronously, it loses the context of "this" On Oct 8, 10:49 am, Wayne <[EMAIL PROTECTED]> wrote: > I've been looking for this quite a bit, today, and haven't quite found > what I'm looking for, so maybe someone can point me in the right > direction. > > I'm performing an AJAX request, and I pass an anonymous function as > the success callback, upon which I will want to do some DOM > modification to the element that triggered the AJAX event. Ordinarily, > $(this) would work fine for doing this, but it seems that $(this) > points instead to something besides a DOM element. > > First, what does $(this) point to when you're inside the AJAX Success > callback. > > Second, what is considered the best practice for accessing the > triggering element after a successful AJAX query? > > Any help is appreciated, > Wayne