Solved

Require a Radio Button be Selected if a Checkbox is

Posted on 2015-02-22
6
68 Views
Last Modified: 2015-02-22
I am really trying to avoid reverting back to javascript for this but I am having a heck of time in jquery.    I have a checkbox that if checked, I need to require ONE of the radio buttons be selected as well.    This works great if there is only one radio button but not so much if there is more than one...

JQUERY
if ($('input:checkbox[value=658]').is(':checked') && ($("##cepftestid").is(":not(:checked)")) ) {
			 alert('You have selected to purchase a CEFP Exam but you have not indicated the date you wish to attend.');
			 var1 = 0;
		}

Open in new window


HTML
<h2><input type="checkbox" name="itemnumber" value="658" data-cost="600.0000" data-options="#cefpexams" class="itemnumber" > Register for an Exam  ($600.00)</h2>


<div id="cefpexams" style="display:none;">
<input name="cefptestid" type="radio" value="00/00/0000" id="cefptestid"  /> I will take the exam at a proctored location<br />
<input name="cefptestid" type="radio" value="12/30/1899" id="cefptestid"  /></TD><TD> December 30, 2015
</div>

Open in new window

0
Comment
Question by:digitalwise
  • 3
  • 2
6 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 40624504
Hi,
there are some typos in your code (e.g. wrong radiobutton ID selector):
if ($('input:checkbox[value=658]').is(':checked') && !($("#cefptestid").is(":checked")) ) {
			 alert('You have selected to purchase a CEFP Exam but you have not indicated the date you wish to attend.');
			 var1 = 0;
		}

Open in new window


And here a sample:
http://jsfiddle.net/EE_RainerJ/yjqxbyj9/

HTH
Rainer
0
 

Author Closing Comment

by:digitalwise
ID: 40624512
Thank you!   I need 5 minutes to breath so I can actually look over JQUERY instead of just finding what I need!
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40624564
Ooooh. You accepted an answer before I had a chance to comment. Rainer's solution won't. Tick the box and select the second radio button, and you'll still get the error!

Your inputs are using the same IDs, and as I've said before, they need to be unique!!
0
DevOps Toolchain Recommendations

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

 

Author Comment

by:digitalwise
ID: 40624570
Ah!   And I only tested it for the first one and it worked.    So if they are unique, how the heck do I make sure ONE of them is selected?    Do I need to reopen this or something??   Ughhh!
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 40624580
Hi,
Chris is right (I thought I tested all options). Nevertheless, another (correct) selector would be:
$('input:radio[name="cefptestid"]')

Open in new window


I have updated the jsFiddle from above as well:
http://jsfiddle.net/EE_RainerJ/yjqxbyj9/

HTH
Rainer
0
 

Author Comment

by:digitalwise
ID: 40624590
Thanks guys!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now