Yetimwork Beyene
Yetimwork Beyene

Reputation: 2337

how to loop through a $.post callback?

how to loop through a $.post callback function?

Here's the code I would like to loop through....

$.post("p.php",{config: config1, test: test1}, function(data)
{

});             

Upvotes: 1

Views: 149

Answers (2)

ShankarSangoli
ShankarSangoli

Reputation: 69915

You can use $.each method to loop through an array or objects. Following are the examples with array and object looping using $.each method.

Looping an array

$.each([52, 97], function(index, value) { 
    alert(index + ': ' + value); 
});

Looping an object

var map = { 
  'flammable': 'inflammable', 
  'duh': 'no duh' 
}; 
$.each(map, function(key, value) { 
    alert(key + ': ' + value); 
});

As you mentioned in the comments the response contains 4 p tags and you need to append them to an li tag on the page, you can try this.

$.post("p.php",
    { config: config1, test: test1}, 
    function(data){ 
      //if you have an to li then use id selector or 
      //if you have a class to li then use class selector
      $('liSelector').append(data);
});  

Upvotes: 1

John Fable
John Fable

Reputation: 1091

$.each(data, function(k,v) {

...

});

Upvotes: 0

Related Questions