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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:53:44+00:00 2026-05-24T12:53:44+00:00

I am using jQuery GET in a loop to obtain several results from the

  • 0

I am using jQuery “GET” in a loop to obtain several results from the server. I want to include the loop index as a fixed parameter to the call back but its not working.

(I followed the advice of this article on how to do it.)

However, the value I get in the call back is completely not what I expect – rather than each loop index value, it is always equal to the exit value of the index.

ie. the code fragment here prints out ’16’ for each execution of the callback. How do I get it to print 1, 2, 3… (I realize the order might be different, that’s fine)

In addition to the code below, I’ve tried several ways to specify the call back function, eg. function(data, textStatus) { return test(data, textStatus, idx); }, 'text'); etc.

How is this supposed to work?

function test(data, textStatus, siteNo)
{
    console.log("siteNo=" + siteNo);
}

function loadConfigLists()
{
    var siteReport;
    // retrieve site configuration
    jQuery.get("svGetSiteConfig.php", function(data, textStatus) 
    {
        // retrieve port configuration for all sites
        for (var idx=1; idx<=15; idx++)
        {
            var probeIP = siteConfigArray[idx].siteIP;
            if (probeIP != "" && probeIP != null)
            jQuery.get("svGetPortInfo.php?svSiteIpAddr=" + probeIP+"&s="+idx, 
                    function(data, textStatus) { test(data, textStatus, idx); }, 'text'); 
            else // IP value is blank
                siteConfigArray[idx].portManifest = null;
        }
        }
    }, 'text'); 
}
  • 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-24T12:53:45+00:00Added an answer on May 24, 2026 at 12:53 pm

    This is a pretty standard problem with closures. When you do this:

    function(data, textStatus) { test(data, textStatus, idx); }
    

    You’re binding a reference to idx but not to the value of idx. So, by the time your callback gets called, the loop will have finished and idx will be 16 in all of the callbacks that you bound.

    The usual solution is to force evaluation of idx through a function call:

    function build_callback(idx) {
        return function(data, textStatus) {
            test(data, textStatus, idx);
        };
    }
    
    // And then...
    
    jQuery.get("svGetPortInfo.php?svSiteIpAddr=" + probeIP+"&s="+idx, build_callback(idx), 'text');
    

    You can also inline the function with a self-executing function if you want to keep it all together:

    for (var idx=1; idx<=15; idx++)
        (function(idx) {
            var probeIP = siteConfigArray[idx].siteIP;
            if (probeIP != "" && probeIP != null)
                jQuery.get("svGetPortInfo.php?svSiteIpAddr=" + probeIP+"&s="+idx, 
                    function(data, textStatus) { test(data, textStatus, idx); }, 'text'); 
            else // IP value is blank
                siteConfigArray[idx].portManifest = null;
        })(idx);
    

    The function call gives you the value of idx when the function is called and that’s the value of idx that you want.

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

Sidebar

Related Questions

Hi I'm using JQuery tabs http://jqueryui.com/demos/tabs/ with search results being returned from my server
Using jQuery I get JSON object that extracts one row from database. That row
I'm using Jquery to submit a form. I want to loop through the form
Hi i want to get radiobutton value using jquery. This is the radiobutton code.
I am trying to get an element using JQuery's selector, but it does not
I make an AJAX request like so using JQuery: $.ajax({ type: GET, url: getvideo.php,
I'm using the jQuery autocomplete plugin to get a list of locations, which works
I am using the FlexiGrid jQuery plug in and I need to get a
I'm using jQuery 1.3.1 and when I try to loop through a form and
I'm using jQuery to setup a timer or interval loop on a few elements

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.