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

  • Home
  • SEARCH
  • 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 7617857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:15:05+00:00 2026-05-31T03:15:05+00:00

Possible Duplicate: jQuery: Return data after ajax call success I wrote a script which

  • 0

Possible Duplicate:
jQuery: Return data after ajax call success

I wrote a script which adds a new div container to the said with the a select field inside. The data for the select field is loaded with an ajax request before. But for some reason the fields are only visible when I output something with alert().

var o = '';
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: webroot + 'items',
                success: function(data) {
                    $.each(data, function(index, value) {
                        o += '<option value="' + index + '">' + value + '</option>';
                    });
                }
            });
            var l = parseInt($('.items .item').length);
            var h = '<div class="item"><span class="bold">Item ' + (l + 1) + '</span><select id="ItemName" name="data[Item][name]">' + o + '</select></div>';

I have actually no idea how to solve this problem. Can you help me?

  • 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-05-31T03:15:06+00:00Added an answer on May 31, 2026 at 3:15 am

    Ajax is asynchronous. Move all of your code into the success function, and it will work. The reason it works with alert is because the alert allows some time to get the AJAX result.

    $.ajax({
        type: 'post',
        dataType: 'json',
        url: webroot + 'items',
        success: function(data) {
            var o = '';
            $.each(data, function(index, value) {
                o += '<option value="' + index + '">' + value + '</option>';
            });
    
            var l = parseInt($('.items .item').length);
            var h = '<div class="item"><span class="bold">Item ' + (l + 1) + '</span><select id="ItemName" name="data[Item][name]">' + o + '</select></div>';
        }
    });
    

    Your code doesn’t actually do anything other than load some html into a variable though. So whatever you were doing with “h”, do it in the success function.

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

Sidebar

Related Questions

Possible Duplicate: jquery ajax call taking too long or something for some reason this
Possible Duplicate: jQuery Ajax always returns “undefined”? I'm trying to come up with a
Possible Duplicate: What does jQuery.fn mean? I have some script on a page trying
Possible Duplicate: Kill Ajax requests using JavaScript using jQuery Here is the simple code
Possible Duplicate: jQuery bind click ANYTHING but ELEMENT I have this HTML <div id=outer>
Possible Duplicate: Nested jQuery.each() - continue/break Here is my code: var steps = $(div#f);
Possible Duplicate: jQuery refresh if PHP file echo's 'true' I have the following div
Possible Duplicate: How do you know when to return false from a jQuery function?
Possible duplicate: jquery change div text The above question is a possible duplicate, but
Possible Duplicate: jQuery ui dialog change title after load-callback I like to change the

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.