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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:29:13+00:00 2026-05-29T10:29:13+00:00

Here is what I have done: <html><head><script type=text/javascript> function loaded(){ var selectElems=document.getElementsByName(select); for(i=0;i<selectElems.length;i++){ var

  • 0

Here is what I have done:

<html><head><script type="text/javascript"> 
function loaded(){
    var selectElems=document.getElementsByName("select");
    for(i=0;i<selectElems.length;i++){
      var elem=selectElems[i];
      elem.onchange=function(){
          alert(elem.selectedIndex);
       }
     }
} </script></head>
<body onload="loaded()">
<select name="select">
      <option>0</option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option> </select>
 <select name="select">
      <option>0</option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option></select>
 <select name="select">
      <option>0</option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option></select>
</body></html>

I need to get selected index of element with JavaScript. But I had a problem with that.
Here is my code: http://jsfiddle.net/aRMpt/139/

  • 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-29T10:29:14+00:00Added an answer on May 29, 2026 at 10:29 am

    The problem is that your elem always points to the last select element. The simplest solution is to use this in the handler instead of the elem variable http://jsfiddle.net/mendesjuan/aRMpt/140/

    function loaded(){
        var selectElems=document.getElementsByName("select");
        for(i=0;i<selectElems.length;i++){
          var elem=selectElems[i];
          elem.onchange=function(){
              // This points to the select element that was changed
              alert(this.selectedIndex);
           }
         }
    }
    

    This doesn’t really tell you what your problem is though. It’s a workaround (though a good one). The reason your code doesn’t work is because your closure function onchange uses the same elem from the closure. By the time your onchange is called, elem points to the last select element. A way to avoid this is to introduce another closure that freezes the elem for your onchange handler http://jsfiddle.net/mendesjuan/aRMpt/142/

    function loaded(){
        var selectElems=document.getElementsByName("select");
        for(i=0;i<selectElems.length;i++){
          var elem=selectElems[i];
          // Freeze the elem variable by creating a function that passes
          // the elem and creates a separate closure for each handler
          elem.onchange= (function(el) {
              // This is the function that will actually be the handler
              return function(){
                 // This points to the select element that was changed
                 alert(el.selectedIndex);
              }
         }) (elem) 
    }
    

    Here’s another example that may help you understand how the above example works.

    // This is a function that returns another function
    // Its only reason is so that we don't use the shared closure 
    // variables from the outer scope, it freezes the el
    // variable at the moment its called and so that it's available
    // when the handler is called
    function createOnChangeHandler(el) {
      return function() {
         alert(el.selectedIndex);
      }
    }
    
    function loaded(){
        var selectElems=document.getElementsByName("select");
        for(i=0;i<selectElems.length;i++){
          var elem=selectElems[i];
          // If we just use elem here, its value will be what it was assigned last
          // in the loop. That is because the handler won't be called until
          // this loop has finished. However, by calling another function, 
          // a new scope is created with the value that we're passing into it
          elem.onchange = createOnChangeHandler(elem);
    }
    

    This freezing of variables could also be accomplished by using Function.bind, but this only works in newer browsers; however, the above link shows you how to make it work for browsers that don’t support it. http://jsfiddle.net/mendesjuan/aRMpt/143/

    function loaded(){
        var selectElems=document.getElementsByName("select");
        for(i=0;i<selectElems.length;i++){
          var elem = selectElems[i];
          elem.onchange = (function (el) {
            alert("Select index: " + el.selectedIndex)
          }).bind(null, el);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope I am not missing something very simple here. I have done a
I feel like GUID/UUID questions have been done to death here, but I can't
Quick question, What have I done wrong here. The purpose of this code is
I have a file that contains this: <html> <head> <title> Hello! - {{ today
I have done enough research and have decided to move the JavaScript to the
I am facing a problem with JavaScript objects. I have some text on a
Okay, what have I done wrong here? I'm trying to .POST data from a
Just wondering what little scripts/programs people here have written that helps one with his
Since upgrading to 2008 I and many people here have noticed that randomly VS
Here I have: Public Structure MyStruct Public Name as String Public Content as String

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.