Reputation: 73
I have 2 radio buttons that I want to throw a validation if both are unchecked. I'm sure my syntax is wrong somewhere and would appreciate help with this, and if there is a better way to do it.
Thanks.
My js fiddle link: [http://jsfiddle.net/squirc77/THQj2/18/]
Upvotes: 1
Views: 479
Reputation: 44740
you are making it to look complex - you can just do this
$('#submit').click(function (e) {
e.preventDefault();
if (!$('#radio1').prop('checked') && !$('#radio2').prop('checked')) {
$('#validate').show();
}
});
Demo ----->
http://jsfiddle.net/THQj2/33/
Upvotes: 1
Reputation: 27
Correct!
$('#submit').click(function(){
if((!$('#radio1').attr('checked')===true) && (!$('#radio2').attr('checked'))===true) {
$('#validate').show();
}
});
Upvotes: 0
Reputation: 895
There is an extra bracket after first true .. it should be like this:
$('#submit').click(function(){
if(!$('#radio1').attr('checked')===true && (!$('#radio2').attr('checked'))===true) {
$('#validate').show();
}
});
Updated jsfiddle here: http://jsfiddle.net/THQj2/31/
Upvotes: 0
Reputation:
Your syntax is wrong (missing (
) .
Here's a fiddle with the correct syntax
Upvotes: 0
Reputation: 122008
You missed (
if( ( !$('#radio1').attr('checked')===true) &&
----^ (!$('#radio2').attr('checked'))===true) {
and you have to add return false also
$('#validate').show();
return false;
Final code
$('#submit').click(function(){
if((!$('#radio1').attr('checked')===true) && (!$('#radio2').attr
('checked'))===true) {
$('#validate').show();
return false;
}else{
$('#validate').hide();
}
});
Upvotes: 1