By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,297 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,297 IT Pros & Developers. It's quick & easy.

Javascript call function once loop finishes and jQuery populates content

P: 3
I'm trying to get my jQuery to work in this flow:

1.) Parse json using $getJson 2.) Loop through returned JSON and create divs for each JSON item 3.) Once done, repeat but instead with another json file and append the results to the existing results.

But as of now I can't call another function AFTER the first loop is done, because the function gets called before the page is populated. How can I finish the populating loop, and then call another function?

I appreciate any help you can give me. Still learning as you can tell.

Expand|Select|Wrap|Line Numbers
  1. function test() {
  2.   var i;
  3.   for(i=0;i<=10;i++) {
  4.     $("div#test").append("<div id='"+i+"'></div>");
  5.   }
  6.   when_loop_is_done();
  7. }
  8. function when_loop_is_done() {
  9.   var i;
  10.   for(i=0;i<=10;i++) {
  11.     $("div#test div#"+i).append("<span>foo</span>");
  12.   }
  13. }
  14.  
Essentially I'm grabbing JSON from a separate page on my server, then looping through and populating the content using variables from that JSON object. The issue is no matter what I do, the second function always gets called before jQuery populates the content. So if I were to call an alert after the loop is done the page would pop up the alert and then load in all of the appended html.
Oct 21 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.