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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:05:35+00:00 2026-06-06T05:05:35+00:00

Summary: I have passed the name of the id select tag into an Array.

  • 0

Summary: I have passed the name of the id select tag into an Array. I want to dynamically assign each Select Item to display the Index of the selected item upon change. This is sent to the output div. Only the third select item is dynamically being triggered. Why?

<script language="JavaScript" type="text/javascript">

$(document).ready(function(){

var NameOfSelect=new Array("a","b","c");

for (i=0;i<NameOfSelect.length;i++){

var sel=NameOfSelect[i];
$("#"+NameOfSelect[i]).change(function () {
           var str = "";
           $("#"+sel+" option:selected").each(function () {
            str += $(this).index() + " "+$("#"+sel).attr("id");
                  });
          $("#output").text(str);
       })
        .trigger('change');

}
</script>

The HTML

<select id="a" >
<option value="0a" >0a</option>
<option value="1a"  >1a</option>
<option value="2a" >2a</option>

</select>


<select id="b" >
<option value="0b"  >0b</option>  
<option value="1b" >1b</option>
<option value="2b" >2b</option>
</select>

<select id="c" >
<option value="0c" >0c</option>
<option value="1c" >1c</option>
<option value="2c" >2c</option>

</select>
<div id=output></div>

The output shows that only “C” is being triggered on change.
Why are “a” and “b” not being similarly dynamically assigned?

  • 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-06T05:05:37+00:00Added an answer on June 6, 2026 at 5:05 am

    This occurs because the variable c gets reassigned with every iteration. At the end of the for-loop, all elements will have a proper event handler, but they will refer to the same c variable inside their function body. This variable will be holding the last value of c, being "c".

    One solution would be to cache the value of c in a closure for every function handler you assign, but in this case it’s much better to simply use this inside the handler to refer to the select element that was targeted by the change event.

    $(document).ready(function(){
        var NameOfSelect = ["a", "b", "c"];
    
        for (var i=0; i < NameOfSelect.length; i++) {
            $("#"+NameOfSelect[i]).change(function () {
                // this refers to the targeted select element
                var select = this;
                var str = "";
                $("option:selected", this).each(function () {
                    // this refers to the selected option
                    str += $(this).index() + " " + $(select).attr("id");
                });
                $("#output").text(str);
            }).trigger("change");
        }
    });
    

    Also, unless you’re using selects with multiple selections, there should only be one option:selected for each select element. Therefore, you could just use find and work with the first (and only) selected option.

    var str = $("option:selected", this).index() + " " + $(this).attr("id");
    $("#output").text(str);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary I have looked over the code the SpiderMonkey 'shell' application uses to create
--Summary (shortened)-- I have a controller that loads a profile object from the corresponding
I have column named summary (tinytext, utf8_turkish_ci). I know it can store 255 byte
Summary A parent can have many children. How do you write a service such
I have an object Title : foo Summary : foo bar Body : this
I have a folder with two files: Awesome.File.20091031_123002.txt Awesome.File.Summary.20091031_123152.txt Additionally, a third-party app handles
I have a dictionary like this: /// <summary> /// Gets the leave entitlement details.
I have an SQL Server 2000 db, and I would like to retrieve summary
I have two identical byte arrays in the following segment of code: /// <summary>
I have a Project model and it has some text attributes, one is summary.

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.