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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:08:17+00:00 2026-06-11T04:08:17+00:00

Can someone please tell me why this isn’t working? var items = <select id=\orderer_search\

  • 0

Can someone please tell me why this isn’t working?

  var items = "<select id=\"orderer_search\" name=\"search\">";
  $.getJSON("https://dev.xxx.com", 
    function(data){
     $.each(data, function(index,item) { 
       items += "<option value='" + item + "'>" + item + "</option>";
     });           
  });

  items += "</select>";

If I place an alert(items) before:

items += "</select>";

The value is:

<select id="orderer_search" name="search">

But if I place an alert in the each loop, each popup is showing the value that has come back from the JSON request.

It is almost like as soon as the each loop is over, it is removing everything I added to the items variable during the each loop. I am new to JQuery so I’m sure it’s something simple.

  • 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-11T04:08:19+00:00Added an answer on June 11, 2026 at 4:08 am

    It’s not removing everything, the JSON request is just not finished when you do items += "</select>";!

    $.getJSON runs asynchronously, meaning that the statement starts the request to the given URL, but it doesn’t wait for the answer, the program flow then just continues on with the next statement (in your case the items += ..., without waiting for $.getJSON to finish! Only when the response arrives, the given success function is called.

    You have two choices in how to solve that problem.
    Alternative 1 would be postponing all the assemblage of the items string until $.getJSON has succeeded:

    $.getJSON("https://dev.xxx.com", 
        function(data){
           var items = "<select id=\"orderer_search\" name=\"search\">";
           $.each(data, function(index,item) { 
               items += "<option value='" + item + "'>" + item + "</option>";
           });
           items += "</select>";
           alert(items); // or whatever else you have to do with items
        }
    );
    

    Alternative 2 would be to make the $.getJSON call non-asynchronous, so that the function waits for the response to come. You’d have to use the $.ajax function, like this:

    var items = "<select id=\"orderer_search\" name=\"search\">";
    $.ajax({
        url: "https://dev.webshop.restore.net.au/_pag/shop/TEST/?
              bc=TEST&sec=order%2F&pag=get_orderers_list",
        dataType: 'json',
        async: false,
        success: function(data) { 
            $.each(data, function(index,item) { 
                items += "<option value='" + item + "'>" + item + "</option>";
            }
        }
    });
    items += "</select>";
    alert(items); // or whatever else you have to do with items
    

    Alternative 1 has the potential to make your script more responsive, if there is anything else to do apart from filling this select; because then the request is running in the background, quasi in parallel to your other code. So I would usually go for that.

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

Sidebar

Related Questions

can someone please tell me why this isn't working? For some reason I need
Can someone please tell me why this isn't working? I would like to be
Please can someone tell me why this isn't working before I defenestrate everything on
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is
Can someone please tell me why this doesn't work? I'm trying to show and
Can someone please tell me whats wrong with this code. I'm not getting complete
Can someone please tell me what's wrong with this sp. The logic seems to
I can't seem to get this text to align vertically. Can someone please tell
Can someone please tell me why this won't work? NSAppleScript* playPause = [[NSAppleScript alloc]
Hi can someone please tell me why this code is incorrect? I'm trying to

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.