Link to home
Start Free TrialLog in
Avatar of mock5c
mock5c

asked on

.inArray or not .inArray with .each

I have a group of checkboxes.  I'd like to only allow one box to be selected for subset of those checkboxes to be selected.  For another subset, more than one can be selected.

For example, if I have these checkbox AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, I want to allow the user to select AAA and BBB or any combination of those (not required to check).  Then for CCC, DDD, EEE, FFF, GGG, HHH, they can only select one of those boxes.  If CCC is currently checked and the user then selects FFF, then CCC will get unselected and FFF gets selected.  However, if AAA and/or BBB were also selected, they remain unchanged when clicking on FFF in this example.

$('#mytype :checkbox').click(function () {
   var exclude = ["AAA","BBB"];
 $('#mytype :checkbox').not(this).removeAttr('checked');
})

Open in new window


No, I know the line above with removeAttr will uncheck all of the boxes except for the one that was selected.  This is not quite what I'm looking for.  So, I was wondering how I can include .inArray in that line or how can I accomplish this with .each
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Hi,
one working example:
http://jsfiddle.net/EE_RainerJ/xmy32m4a/

Use this code:
$('#mytype :checkbox').click(function () {
   var exclude = "#AAA, #BBB";
 $('#mytype :checkbox').not($(this).add(exclude)).removeAttr('checked');
});

Open in new window

Avatar of mock5c
mock5c

ASKER

I've look at the example in jsfiddle.  The only issue I see is that if you select/deselect AAA or BBB, and one of the other boxes is checked, it will also deselect that other box.  The other boxes should not change in this case.
ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mock5c

ASKER

Works perfectly.  An elegant solution.