Solved

.inArray or not .inArray with .each

Posted on 2015-01-14
4
87 Views
Last Modified: 2015-01-14
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
Comment
Question by:mock5c
  • 2
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 40550008
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
 

Author Comment

by:mock5c
ID: 40550020
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
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 40550033
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
 

Author Closing Comment

by:mock5c
ID: 40550082
Works perfectly.  An elegant solution.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now