Norman
Norman

Reputation: 6365

Accessing $(this) after success:function() when using $.ajax

How can I access the value of $(this) after success: function() when using jquery? No matter what I've tried, it appears I cant do it.

$('.add').click(function() {
//etc etc
    $.ajax({
            type:"GET",
            url:"/",
            data:data,
            dataType: 'json',
            beforeSend:function(html){
                //Nothing here right now
            },
            success: function(){
                $(this).parent("div").after("<div class='flag'>I'm the new one.</div>");
            },

<div id="container">

    <div class="h1" data-id="1">Teachers <span class="add" data-id="US01">Add New</span></span>
    </div>
    <div class="flag">Flag</div>
    <div class="edit">Edit</div>
    <div class="h2" data-id="2">Who is your favorite Math teacher? <span class="add" data-id="US02">Add New</span></div>
    <div class="flag">Flag</div>
    <div class="edit">Edit</div>
        <br>
    <div class="h1" data-id="8">Restaurants <span class="add" data-id="US10">Add New</span></div>
    <div class="flag">Flag</div>
    <div class="edit">Edit</div>
    <div class="h2" data-id="9">Which is your favourtie restaurant in town? <span class="add" data-id="US20">Add New</span></div>
    <div class="flag">Flag</div>
    <div class="edit">Edit</div>

</div>

Upvotes: 2

Views: 113

Answers (3)

David Hewitt
David Hewitt

Reputation: 837

This is a problem with scope. You need to assign this to a variable within the scope of where it is accessible. Then use that variable in child functions.

Note I've used _that

This should work, please comment if it doesn't.

 $('.add').click(function() {
    var _that = this;
    $.ajax({
        type:"GET",
        url:"/",
        data:data,
        dataType: 'json',
        beforeSend:function(html){
            //Nothing here right now
        },
        success: function(){
            $(_that).parent("div").after("<div class='flag'>I'm the new one.</div>");
        },
    });
 });

Upvotes: 0

Michael Kisilenko
Michael Kisilenko

Reputation: 501

"this" inside your ajax success is not the same "this" outside.

Different scopes...

There is a nice solution for this question.

$('.add').click(function() {
var that = $(this);
//do whatever you want :]

 $.ajax({
        type:"GET",
        url:"/",
        data:data,
        dataType: 'json',
        beforeSend:function(html){
            //Nothing here right now
        },
        success: function(){
            that.parent("div").after("<div class='flag'>I'm the new one.</div>");
        },

  });
});

Upvotes: 3

Richard Dalton
Richard Dalton

Reputation: 35793

Save this to a variable before making the ajax call and use that instead:

$('.add').click(function() {
    var self = this; // save this as self
    //etc etc
    $.ajax({
            type:"GET",
            url:"/",
            data:data,
            dataType: 'json',
            beforeSend:function(html){
                //Nothing here right now
            },
            success: function(){
                // use self instead of this
                $(self).parent("div").after("<div class='flag'>I'm the new one.</div>");
            },

Upvotes: 4

Related Questions