squirc77
squirc77

Reputation: 73

Syntax for && operator with jquery

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

Answers (5)

Adil Shaikh
Adil Shaikh

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

Maciel Sousa
Maciel Sousa

Reputation: 27

Correct!

$('#submit').click(function(){
    if((!$('#radio1').attr('checked')===true) && (!$('#radio2').attr('checked'))===true) { 
  $('#validate').show();
}

});

Upvotes: 0

mara-mfa
mara-mfa

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

user2050393
user2050393

Reputation:

Your syntax is wrong (missing ( ) . Here's a fiddle with the correct syntax

Upvotes: 0

Suresh Atta
Suresh Atta

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();
    }

});

Updated Fiddle

Upvotes: 1

Related Questions