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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:43:56+00:00 2026-05-27T01:43:56+00:00

I have the following loop. The goal is to automatically show text boxes when

  • 0

I have the following loop. The goal is to automatically show text boxes when a user chooses certain options in a dropdown list.

For some reason, in the following code, the loop assigns the “sponsor” field to the associated array for “blurb”. I cannot figure out why. How can I make this work?

Thanks so much.

function add_fields_on_change ()
{
    var map = {
        "sponsor"   :   Array("New Sponsor", "new_sponsor"),
        "blurb"     :   Array("New Blurb Suggestion", "new_blurb")
    };
    for (field in map)
    {
        alert($('.bound[name='+field+']').val()); //alerts as expected
        $('.bound[name='+field+']').change(function() {
            alert(map[field][0]); //alerts "New Blurb Suggestion" for both "sponsor" and "blurb" fields
            if ($(this).val() == map[field][0])
            {
                 $('.hidden[name='+map[field][1]+']').show();
            }
        });
    }
}
  • 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-27T01:43:56+00:00Added an answer on May 27, 2026 at 1:43 am

    The reason is that the event handler you’re creating (which is a closure) has an enduring reference to the field variable, not a copy of it as of when the function was created. So all of the event handlers that get created will see the same value of field (the last value assigned to it).

    Because you’re already using jQuery, the easiest solution is probably to use $.each:

    function add_fields_on_change ()
    {
        var map = {
            "sponsor"   :   Array("New Sponsor", "new_sponsor"),
            "blurb"     :   Array("New Blurb Suggestion", "new_blurb")
        };
        $.each(map, function(field, value) {
            alert($('.bound[name='+field+']').val()); //alerts as expected
            $('.bound[name='+field+']').change(function() {
                alert(map[field][0]); //alerts "New Blurb Suggestion" for both "sponsor" and "blurb" fields
                if ($(this).val() == map[field][0])
                {
                     $('.hidden[name='+map[field][2]+']').show();
                }
            });
        });
    }
    

    That works because the event handlers close over the call to the iterator function you’re passing into each, and so they each get their own field argument, which never changes.

    If you wanted to do it without using $.each for whatever reason, you’d do something like this:

    function add_fields_on_change ()
    {
        var map = {
            "sponsor"   :   Array("New Sponsor", "new_sponsor"),
            "blurb"     :   Array("New Blurb Suggestion", "new_blurb")
        };
        var field;
        for (field in map)
        {
            alert($('.bound[name='+field+']').val()); //alerts as expected
            $('.bound[name='+field+']').change(createHandler(field));
        }
    
        function createHandler(f) {
            return function() {
                alert(map[f][0]); //alerts "New Blurb Suggestion" for both "sponsor" and "blurb" fields
                if ($(this).val() == map[f][0])
                {
                     $('.hidden[name='+map[f][3]+']').show();
                }
            };
        }
    }
    

    It’s the same principle, the event handler closes over the call to createHandler and uses the f argument, which is unique to each call, and which we’re never assigning a new value to.

    Note that I declared the field variable. That declaration was missing from your original code, which means you were falling prey to the Horror of Implicit Globals.

    More reading:

    • Closures are not complicated
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation: I have a certain function that runs a loop
I have the following loop: for(var myScreen in wizardScreens){ if(step==index)$(myScreen).show(); else $(myScreen).hide(); index++; }
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have the following loop in my viewDidLoad: for(int i=1; i<[eventsArray count]; i++) {
I have the following loop inside an function init(); which is executed onload, I
Say I have the following loop: i = 0 l = [0, 1, 2,
I have the following For loop in a batch file: for /R c:\test\src %%i
I have following exception in the foreach loop Unable to cast object of type
I have the following command which will loop over all the subdirectories in a
I have a following code in a most inner loop of my program struct

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.