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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:07:06+00:00 2026-06-15T16:07:06+00:00

Possible Duplicate: Javascript infamous Loop problem? I have the following code: function test() {

  • 0

Possible Duplicate:
Javascript infamous Loop problem?

I have the following code:

function test() {

   var columns = options.columns;

for (var i =0; i < columns.length; i++) {
    if (columns[i].type === "number") {
        var field = columns[i].field;
        columns[i].footerTemplate = function(data) { return buildFooter(data, field);     };
      }
  }
}

function buildFooter(data, field) {
   alert(field);
 }

A library function calls the footerTemplate function (which in turn call buildFooter). The alert in buildFooter states that field is always the same value (the last value iterated in for loop of test.) How can buildFooter be called with the appropriate field value (i.e.

 columns[0].footerTemplate = function(data) { return buildFooter(data, columns[0].field);} 

and

 columns[1].footerTemplate = function(data) { return buildFooter(data, columns[1].field);}

and so on…

  • 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-15T16:07:07+00:00Added an answer on June 15, 2026 at 4:07 pm

    Try:

    columns[i].footerTemplate = (function(field) {
        return function (data) {
            buildFooter(data, field);
        };
    })(field);
    

    It immediately executes a function that returns a new function that has the correctly bound field variable. It’s definitely a scope issue that is a problem with loops, so using an immediately invoked function to create a new scope and making the correct variables available. This is definitely a duplicate of Javascript infamous Loop issue? though

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

Sidebar

Related Questions

Possible Duplicate: Javascript infamous Loop problem? I have the following: function test(a,b,c){ console.log(a+b+c); }
Possible Duplicate: Javascript Closure Problem In the following code, TrueThis.aChoices[i]['CallBack'] is a function when
Possible Duplicate: Javascript infamous Loop problem? With the for loop, it does nothing. Without
Possible Duplicate: Javascript infamous Loop problem? I am having troubles trying to access a
Possible Duplicate: Javascript infamous Loop problem? For some reason I get 6 inside my
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} AFAIK, there
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What is
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} I would
Possible Duplicate: JavaScript: Know when an image is fully loaded I have this code:

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.