0

I want to ajax an array of urls, like this

var urls = [url1, url2, url3];
var ids = [id1, id2, id3];

function test() {
        for(var i = 0 ; i<urls.length; i++){
        $.ajax({
          url: **urls[i]**,
          type:'POST',
          success: function(data){
             $('**ids[i]**').html($(data).find('.....').text());
          }
        });
       }
    }

For 1 url i want to use 1 id ex:ids[0] = urls[0] ....

1
  • 2
    And what is your problem? Commented Oct 14, 2014 at 5:12

1 Answer 1

3

You can't access i inside the success handler because it is a closure variable updated in a loop, which will give you wrong results...

so try

var urls = [url1, url2, url3];
var ids = [id1, id2, id3];

function test() {
    $.each(urls, function (i, url) {
        $.ajax({
            url: url,
            type: 'POST',
            success: function (data) {
                //make sure the ids has `#` prefix 
                $(ids[i]).html($(data).find('.....').text());
            }
        });
    });
}
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.