?
Solved

.inArray or not .inArray with .each

Posted on 2015-01-14
4
Medium Priority
?
106 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

762 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