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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:45:28+00:00 2026-06-03T14:45:28+00:00

I was trying to check check-boxes using array in jquery using multiple select options

  • 0

I was trying to check check-boxes using array in jquery using multiple select options
If, lets say Option 1 and Option 3 is clicked, checkboxes which id is set in array will become checked and if removed unchecked.

<select id="lab_abbr" multiple >
  <option value="o1">Option 1</option>
  <option value="o2">Option 2</option>
  <option value="o3">Option 3</option>
</select>

and checkboxes

<input type="checkbox" name="lab[one]" id="lab_one" />
<input type="checkbox" name="lab[two]" id="lab_two" />
<input type="checkbox" name="lab[three]" id="lab_three" />
<input type="checkbox" name="lab[four]" id="lab_four" />
<input type="checkbox" name="lab[five]" id="lab_five" />
<input type="checkbox" name="lab[six]" id="lab_six" />

and last jquery

$(document).ready(function() {
    $("#lab_abbr").live("change", function(e)  {
        var o1 = ['lab_one', 'lab_three'];
        var o2 = ['lab_two', 'lab_six'];
        var o3 = ['lab_four, 'lab_five'];


    });
});

Regards,
Besmir

  • 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-03T14:45:32+00:00Added an answer on June 3, 2026 at 2:45 pm

    Give a man fish…

    var mapping = { 'o1' : ['lab_one', 'lab_three'], 'o2' : ['lab_two', 'lab_six'], 
                    'o3' : ['lab_four', 'lab_five'] };
    
    $("#lab_abbr").on("change", function () {
      $.each( this.options, function () {
        $( "#" + mapping[ this.value ].join(", #") ).prop("checked", this.selected); 
      });
    });
    

    Demo: http://jsbin.com/onapem/edit#javascript,html

    Teach a man to fish…

    Let’s break it down now and see what each part does.

    We start off by creating an object which will serve as a sort of associative array. Essentially, we get to associate one value to another. In this case, we want to associate the value of the selected option (be it "o1" or "o2"), to a series of checkboxes:

    var mapping = { 'o1' : ['lab_one', 'lab_three'] };
    

    Next we bind our logic to the change event of our select menu using the $.on method:

    $("#lab_abbr").on("change", function(){
      /* React to the change */
    });
    

    Anytime this menu undergoes a change, we want to cycle over its options. We’re not yet worried about whether or not the options are selected – we just want to access every one of them. We do so by using the $.each iterator method:

    $.each( this.options, function(){
      /* Do something with each individual <option /> */
    });
    

    Within this block, we want to gather up those checkbox id’s that are associated with this option value. For instance, if we wanted all values associated with "o1" (which would represent the first option through our $.each, we could do this:

    alert( mapping['o1'] ); // ['lab_one', 'lab_three']
    

    We’re going t do this dynamically though. Within our $.each, the keyword this always refers to the current <option /> being handled. We can use its value to look up any fields associated with it:

    mapping[ this.value ]; // returns an array
    

    We would like to convert this into a CSS selector to pass jQuery, so we use the .join() method on the returned array to create a string:

    mapping[ this.value ].join(", #");
    

    The .join() method accepts a string to add between each value. In the case above, our first iteration through will return in the following string:

    "lab_one, #lab_three"
    

    Note the , # seperating the two values from the array. We need another # in the front of this string so that #lab_one will also be selected. This was solved by concatenating the character onto the resulting string:

    $( "#" + mapping[ this.value ].join(", #") )
    

    Lastly, we call the $.prop() method which allows us to set a property value on the element. We’re going to set the checked property.

    With the $.prop() method, you pass in the property you would like to set, and the value you would like to set it to.

    $( /*...*/ ).prop("checked", this.selected );
    

    Remember, this represents whichever <option /> we’re currently accessing in this iteration of $.each(). The <option /> has a native property called selected which returns either true or false – indicating if it’s selected or not. We use this to tell jQuery what the value of "checked" should be.

    So if the option being handled is selected, so will each one of the checkboxes it is associated with. And likewise, if the option being handled is not selected, neither will the checkboxes it is associated with.

    Want a smaller fish?

    If you wanted to shorten this all a bit more, you could store the jQuery selector directly in the object:

    var mapping = { 'o1' : '#lab_one, #lab_three', 'o2' : '#lab_two, #lab_six',
                    'o3' : '#lab_four, #lab_five' };
    
    $("#lab_abbr").on("change", function () {
      $.each( this.options, function () {
        $( mapping[ this.value ] ).prop( "checked", this.selected ); 
      });
    });
    

    Demo: http://jsbin.com/onapem/2/edit

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

Sidebar

Related Questions

I am trying to delete multiple records using check boxes. I've loaded each check
I am trying to check checkboxes based on a select option. So, if the
I am trying to check all the checkboxes on a webform (aspx) page, which
I'm using mvc and I'm trying to loop through a array of checkboxes, but
I'm trying to build a list of checkboxes by using jQuery.tmpl It'll list an
I am trying to figure out how I can delete multiple records using check
I'm trying to check a series of check boxes depending on the results I
I am trying to have 2 check boxes in rows from first 2 columns
I am trying to post an array full of checkboxes and to open it
The thing I'm trying is to check all those checkboxes, when "cbkomplet" is checked.

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.