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 The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Arkady Rodestve
  • 147
  • 1
  • 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 The GPT Wizard 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,950
  • 5
  • 68
  • 101
  • 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 The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • yea i did it , too.. but nothing.. it is strange.. he doesnt see 'answer_text', but check is passed – Arkady Rodestve Sep 06 '11 at 09:11
  • So debug by adding such alert: `alert("answer: " + answer_text + " (length " + answer_text.length + ")\nchecked count: " + $("input[name='answer[scale]']:checked").length);` and see what you get. – Shadow The GPT Wizard Sep 06 '11 at 09:56
1

Try to wrap it in proper braces and check.

if ((answer_text === '') || (undefined === $("input[name='answer[scale]']:checked").val()))
Vins
  • 8,849
  • 4
  • 33
  • 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
  • 471
  • 1
  • 6
  • 14