Solved

.inArray or not .inArray with .each

Posted on 2015-01-14
4
90 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery Scroll Speed 3 32
gif banner animation loads randomly in mobile 3 81
Asp.net mvc 5 5 43
How can I echo a PHP variable in AJAX function? 7 28
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
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 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…

919 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

13 Experts available now in Live!

Get 1:1 Help Now