2

This my code:

$(document).ready(function() {
    $('.submit').click(function() {
        var answer_text = $("#answer_text").val();
        if (answer_text === '' || undefined === $("input[name='answer[scale]']:checked").val()) {
            alert('error!!');
            return false;   
        }
        else {
            alert('yeah! cool baby!');
        }
    }
});

Problem: jQuery doesn't see the ||. I don't know what to do. I tried to do something like:

if

   else if

else

or

if 

  else

    if

    else

Don't know what to do. please help me, maybe some error and mistakes with OR operator? or what?

Shadow Wizard Says No More War
  • 64,101
  • 26
  • 136
  • 201
Arkady Rodestve
  • 147
  • 2
  • 8
  • @Jeroen `===` is the **identity** operator, while `==` is the **equality** operator. See the accepted answer [here](http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use) for great stuff about the difference. – Shadow Wizard Says No More War Sep 06 '11 at 09:11
  • Nice, never saw that one before. – Jeroen Sep 06 '11 at 09:13

4 Answers4

2

i guess what you wanted to do is

 if (answer_text === '' ||  $("input[name='answer[scale]']:checked").val()==="undefined"){

you have got the operands on the wrong side of the operator

Rafay
  • 30,714
  • 5
  • 66
  • 99
  • i tired to do this , too. but hmm.. nothing. answer_text passed but check does not..;/ If check passed, answert_text does not ;/ – Arkady Rodestve Sep 06 '11 at 09:09
2

To know if no checkbox was checked just use the length, no need to mess with the value:

if (answer_text === '' || $("input[name='answer[scale]']:checked").length === 0) {
    //answer text is empty and no answer scale checkbox was checked
}
Shadow Wizard Says No More War
  • 64,101
  • 26
  • 136
  • 201
1

Try to wrap it in proper braces and check.

if ((answer_text === '') || (undefined === $("input[name='answer[scale]']:checked").val()))
Vins
  • 8,659
  • 4
  • 32
  • 53
0
    $(document).ready(function() { 
        $('.submit').click(function() { 
                    var answer_text = $("#answer_text").val(); 


                    if (answer_text == ''){
                    if ( undefined === $("input[name='answer[scale]']:checked").val()){ 
                        alert('error!!'); 
                        return false;    
                    } 

                    else{ 
                        alert('yeah! cool baby!'); 

                    } 
                    } 

                    else{ 
                        alert('yeah! cool baby!'); 

                    } 
} 

} 

This is not the fastest way but it will do it...

GuyFromOverThere
  • 461
  • 1
  • 6
  • 14