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
Give a man fish…
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:
Next we bind our logic to the
changeevent of ourselectmenu using the$.onmethod: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
$.eachiterator method: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:We’re going t do this dynamically though. Within our
$.each, the keywordthisalways refers to the current<option />being handled. We can use its value to look up any fields associated with it: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: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:Note the
, #seperating the two values from the array. We need another#in the front of this string so that#lab_onewill also be selected. This was solved by concatenating the character onto the resulting string:Lastly, we call the
$.prop()method which allows us to set a property value on the element. We’re going to set thecheckedproperty.With the
$.prop()method, you pass in the property you would like to set, and the value you would like to set it to.Remember,
thisrepresents whichever<option />we’re currently accessing in this iteration of$.each(). The<option />has a native property calledselectedwhich returns eithertrueorfalse– 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:
Demo: http://jsbin.com/onapem/2/edit