1 Replies - 1834 Views - Last Post: 10 August 2011 - 08:22 AM

#1 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Use $(this) is ajax success function

Posted 10 August 2011 - 06:54 AM

Is there a way I can get what I'm trying to do working. This is the code I want to work:

$(document).ready(function(){
	$('.editClass').click(function(){
		$.ajax({
			type: "POST",
			url: "ajaxForumQuote.php",
			data: "postKey=" + $(this).attr('id'),
			success: function(html){
				$("#post-" + $(this).attr('id')).html("<textarea>" + html + "</textarea>");
			}
		});
	});
 });



Everything works fine up until I do the success function. This in particular:
$("#post-" + $(this).attr('id'))



I want the $(this).attr('id') here to represent the same thing as it does in the data statement 2 lines above.

The problem is, and I know this because in the success function I tried doing an alert message for $(this).attr('id'), $(this).attr('id') is undefined. That makes sense, it's a scope issue, correct? Problem is, I don't know what to do so that I can use it in that success function.

Any help would be appreciated. Thank you!

This post has been edited by eZACKe: 10 August 2011 - 06:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Use $(this) is ajax success function

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Use $(this) is ajax success function

Posted 10 August 2011 - 08:22 AM

I think you can create a variable inside the click handler, but outside of the ajax call:
$(document).ready(function(){
    $('.editClass').click(function(){
        var id = $(this).attr('id');
        $.ajax({
            type: "POST",
            url: "ajaxForumQuote.php",
            data: "postKey=" + id,
            success: function(html){
                $("#post-" + id).html("<textarea>" + html + "</textarea>");
            }
        });
    });
});


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1