• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 120
  • Last Modified:

.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
0
mock5c
Asked:
mock5c
  • 2
  • 2
1 Solution
 
Rainer JeschorCommented:
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

0
 
mock5cAuthor Commented:
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.
0
 
Rainer JeschorCommented:
Hi,
sorry - that one I forgot to test.
I updated the jsFiddle. The click function should just be activated on the checkboxes excluding "AAA" and "BBB".
http://jsfiddle.net/EE_RainerJ/xmy32m4a/1/
Sorry for that.
0
 
mock5cAuthor Commented:
Works perfectly.  An elegant solution.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now