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 9233087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:28:07+00:00 2026-06-18T06:28:07+00:00

I just started learning AJAX and have made a simple script. Below is the

  • 0

I just started learning AJAX and have made a simple script. Below is the script.

<!doctype html>
<html>
<head>
    <title>Welcome to the forum</title>
    <script>
        var XMLHttpRequestObject = false;

        if(window.XMLHttpRequest)
        {
            XMLHttpRequestObject = new XMLHttpRequest();
            XMLHttpRequestObject.overrideMimeType("text/xml");
        }
        else
        {
            document.getElementById("text").innerHTML = "Sorry, your browser does not support AJAX!";
        }

        function getData(dataSource)
        {
            XMLHttpRequestObject.open("GET", dataSource);
            XMLHttpRequestObject.onreadystatechange = function()
            {
                if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
                {
                    responseData = XMLHttpRequestObject.responseXML;
                    responseDataList = responseData.getElementsByTagName("os");
                }
            }

            XMLHttpRequestObject.send(null);
        }

        function displayMenuItems(dataSource,selectID)
        {   
            getData(dataSource);

            var i;
            for(i=0; i<responseDataList.length; i++)
            {
                document.getElementById(selectID).options[i] = new Option(responseDataList[i].firstChild.data);
            }
        }

    </script>
</head>

<body>
    <form>
        <select size="1" id="os">
            <option>Select and OS</option>
        </select>
        <input type="button" value="Display OS list" onclick="displayMenuItems('os.xml','os')" />
    </form>

    <div id="text">The Operating System you use will appear here!</div>
</body>

When I click the button, what I expect is to show the list of OSes in the HTML list , but it occurs only after clicking the button twice. I can’t figure out what the problem is! 🙁

Below is the XML script:

<?xml version="1.0"?>
<osList>
    <os>Ubuntu</os>
    <os>Fedora</os>
    <os>Windows</os>
</osList>
  • 1 1 Answer
  • 0 Views
  • 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-06-18T06:28:08+00:00Added an answer on June 18, 2026 at 6:28 am

    The problem is a timing issue. The function displayMenuItems calls getData and then right away tries to iterate over responseDataList, but this list isn’t fetched yet. AJAX doesn’t block, so getData doesn’t wait for the server to respond, and returns before the responseDataList is initialized. You’ll need to call the loop in displayMenuItems after the data is fetched. You can do this by moving it into another function and calling it from the onreadystatechange callback function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning using AJAX with Codeigniter. On my view, I have a
I´ve just started learning GWT and I´m trying to implement http://gwt.google.com/samples/Showcase/Showcase.html#!CwFileUpload and but failing
Just started learning Haskell. I have an empty source file with this inside: pe
I just started learning C, and I am looking for a simple tool for
I just started learning C++ couple of weeks ago. So now I have this
I just started learning portlet and got stuck in the first place. I have
I have just started learning Rails and I'm trying to build a post/like type
I have some php and javascript experience and just started learning curl. Been messing
hi everyone im just started learning ajax. im trying to get the return value
Just started learning Jquery - here's my problem: I have six divs on a

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.