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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:57:28+00:00 2026-05-17T17:57:28+00:00

i have this code to populate the option in my select box(i have 7

  • 0

i have this code to populate the option in my select box(i have 7 select box in this page) with json response from the server(ajax)

for (j=1; j <= 7; j++){

            $.getJSON('mobile_json.php', {id:j}, function(data) {
                var select = $('#userAccess' + j);              
                var options = select.attr('options');
                $.each(data, function(index, array) {                   
                    options[array['Id']] = new Option(array['Name']);
                });  
            });  
    }   

The problem is after the calling to the ajax, the j equal to 8 ,
what i want is when the function deal with the json array, is set it to the right select box like : userAccess1 , userAccess2 … userAccess7,

how can i deal with it?

  • 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-17T17:57:29+00:00Added an answer on May 17, 2026 at 5:57 pm

    It is because you are creating closures in a loop. This helped me to understand it better (especially example 5).

    One way to solve this is to create an anonymous function and execute it immediately :

    for (j=1; j <= 7; j++){
        (function(index) {
            $.getJSON('mobile_json.php', {id:index}, function(data) {
                var select = $('#userAccess' + index);              
                var options = select.attr('options');
                $.each(data, function(index, array) {                   
                    options[array['Id']] = new Option(array['Name']);
                });  
            });
         })(j)
     }  
    

    This “captures” the current value of j. If you don’t do this, by the time the closure (the callback function to the Ajax request) is executed, the loop is already finished, leaving j with the value of 8.
    That’s the tricky thing about closures 🙂

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

Sidebar

Related Questions

I am using the code below to populate a select list from fields in
I have the following code which loads a xml file to populate a dropdown.
I have a PHP function that returns an array. This is the output of
I am trying to populate an HTML select list using an array of customer
Am new to this and just trying to submit dynamically populated select list on
I'm trying to populate a second dropdown in a dynamic way based on a
EDIT: The drop down menus have the following listed in them: Typing Course Daily
I am trying to send a variable with jQuery's getJSON function but it doesn't
I am having a strange issue with a collection_select I am using in an
little background: currently putting together a website that is selling products, many of which

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.