Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 800847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:21:06+00:00 2026-05-14T23:21:06+00:00

My script works very strange, I get all the values, but when I want

  • 0

My script works very strange, I get all the values, but when I want to check them by alerting them – first one gives “undefined”, and other ones is correct. But if I don’t make any alert all variables shows “undefined”. My code:

var arr = new Array();

function fetchData(){
    $.get("post.php", 
        function(data){ 
            arr.img = $(data).find("img").attr("src");
            arr.link = $(data).find("td:last a").attr("href");
            arr.title = $(data).find("td:last a:first b u").text();
            arr.post = $(data).find("td:last span").text();
        }
    );

}

// On page load fetch 4 times and make bar
for (i = 0; i < 4; i++){
    var str;
    fetchData();
    alert(arr.img);
    alert(arr.link);
    alert(arr.title);
    alert(arr.post);

    str = "<td><tr><td><a href='" + arr.link + "'><img src='" + arr.img + "' width='100' height='100' /></a></td></tr><tr><td><strong>" + arr.title + "</strong> - " + arr.post + "</td></tr></td>";

    $("div").append(str);
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:21:07+00:00Added an answer on May 14, 2026 at 11:21 pm

    Your for loop runs before the response is received.

    The reason why the last 15 times work is that by the time you dismiss the first alert, the response has been received.

    Place the alerts in the callback, and you’ll see what I mean.

    var arr = new Array();
    
    function fetchData(){
        $.get("post.php", 
            function(data){ 
                arr.img = $(data).find("img").attr("src");
                arr.link = $(data).find("td:last a").attr("href");
                arr.title = $(data).find("td:last a:first b u").text();
                arr.post = $(data).find("td:last span").text();
    
                var str;
                alert(arr.img);
                alert(arr.link);
                alert(arr.title);
                alert(arr.post);
    
                str = "<td><tr><td><a href='" + arr.link + "'><img src='" + arr.img + "' width='100' height='100' /></a></td></tr><tr><td><strong>" + arr.title + "</strong> - " + arr.post + "</td></tr></td>";
    
                $("div").append(str);
            }
        );
    
    }
    
    // On page load fetch 4 times and make bar
    for (i = 0; i < 4; i++){
                fetchData();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's my first time dealing with cookies in JavaScript and the below script works
I have this script that works, but I need to change the format of
A very strange thing is happening. I am running a script on a new
i have this very simple download page to get an xml file. the script
Python optparse works very good when script usage is something like this %prog [options]
I'm having a very strange error. I run a perl script which executes linux
I am using a bash script to launch a ruby script. this works very
The following script works fine: $(#regform).validate().showErrors({username:message}); After I changed the script to the below
The following script works to open Firefox's location/awesome bar from anywhere using control-l ,
Can anyone tell me why this bash script works if I cut and paste

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.