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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:51:33+00:00 2026-06-02T08:51:33+00:00

When people selected the dropdown and then the checkbox will be auto checked if

  • 0

When people selected the dropdown and then the checkbox will be auto checked if the selected value of dropdown is not empty. But I met “sel[i] is undefined” error.

Javascript part:

var chk = document.getElementsByTagName("input");
var sel = document.getElementsByTagName("select");

    for (var i=0; i< sel.length; i++) {
      sel[i].onchange = function(){

        if (sel[i].value !== ''){
        chk[i].checked  = true;

    }
    else{
        chk[i].checked = false;

    }
  };
}

HTML part:

<form name="form1">
<div class="container">
<input id='checkbox_id1' name='checkbox_id1' type='checkbox' value='1' /></label>&nbsp;

Select
      <label>
          <select id="select_id1" name="select">
            <option value=""></option>
            <option value="111">111</option>
            <option value="222">222</option>
          </select>
          </label>

    <hr>
<input id='checkbox_id2' name='checkbox_id1' type='checkbox' value='1' /></label>&nbsp;


Select
      <label>
          <select id="select_id2" name="select">
            <option value=""></option>
            <option value="111">111</option>
            <option value="222">222</option>
          </select>
          </label>
</div>    

jsfiddle: http://jsfiddle.net/planetoid/GwEuu/

  • 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-02T08:51:36+00:00Added an answer on June 2, 2026 at 8:51 am

    Your value of i is not what you think it is because the for loop has run to completion before any of the event handlers are actually called, thus i is the value at the end of the forloop.

    To capture the appropriate value of i for each event handler, you can use a self executing function like this that captures it in a closure for each specific event handler:

    var chk = document.getElementsByTagName("input");
    var sel = document.getElementsByTagName("select");
    
    for (var i = 0; i < sel.length; i++) {
        (function(i) {
            sel[i].onchange = function(){
                chk[i].checked = sel[i].value !== '';
            }
        })(i);
    }
    

    Working demo: http://jsfiddle.net/jfriend00/yds6w/

    FYI, I’ve also simplified the .checked assignment code.


    But, after looking at your code a bit, I believe this may be a better way of implementing it that doesn’t need the self executing function. Rather than rely on perfectly parallel arrays of checkboxes and input tags, this just converts the ID of the select to the ID of the corresponding checkbox and uses this to reference the select element. So, i is not used in the event handler.

    var sel = document.getElementsByTagName("select");
    
    for (var i = 0; i < sel.length; i++) {
        sel[i].onchange = function(){
            var checkboxId = this.id.replace("select_", "checkbox_");
            document.getElementById(checkboxId).checked = this.value !== '';
        }
    }
    

    ​
    Working demo: http://jsfiddle.net/jfriend00/g3UQG/

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

Sidebar

Related Questions

I'm developing an app which I will give to a selected number of people
People, I already searched a lot but I'm not finding my answer.. I did
So if Set up a DropDownList where Text: people names (string) and Value: studentID
I'm trying to set up a form with multiple dropdown lists, that will allow
I'm trying to change the form action based on the selected value from a
I have a dropdown list in which I select how many people are going
I have seen a TON of people reporting this issue online, but no actual
I have two tables like these: CREATE TABLE people ( id INT NOT NULL,
I'm not finding Apple's documentation very helpful for actually getting data with a people
I've read a lot of people discussing nested lists, but I was wondering how

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.