0

I wish to replace values in each div class with my ajax result but i cannot seem to append my table result to the individual div class during the loop. I know somehow the $(this).append(table) is placed wrongly because it is not outside of the ajax request. How can i modify this to get the effect i wanted?

my script is as such:

$('.developer_badgesarea').each(function(){
    // get the div class value to perform ajax
    var player_id = $(this).html();
    var table;

    // if condition to conduct ajax
    if(player_id != 'None'){

        $.ajax({
            // ajax stuff here
            success: function(result){
                //table created here
                $(this).append(table);
            }
        });
    }
});
3
  • Noticed a missing } for the if block. Commented Apr 5, 2011 at 5:46
  • @experiment it is a html table that i have created using my result. Commented Apr 5, 2011 at 5:51
  • have you added table to DOM??? Commented Apr 5, 2011 at 5:55

1 Answer 1

3

Issue is your "this" reference inside the ajax success function. It references the callback function, instead of the dom element you are intending it to be referenced.

$('.developer_badgesarea').each(function(){
    // element reference to your div, that you'll access inside your ajax call
    var elm = $(this);
    // get the div class value to perform ajax
    var player_id = elm.html(); 
    var table;

// if condition to conduct ajax
if(player_id != 'None'){

    $.ajax({
        // ajax stuff here
        success: function(result){
            //table created here
            elm.append(table);
        }
    });
}

});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.