1

I have the following code:

$('.questionAlternative').change(function() {
    var nestedQuestion = $(this).attr('nestedQuestion');
    if(nestedQuestion != null || nestedQuestion != "") {
        $("#"+nestedQuestion).show();
    } else {
        $(this).parent().next('.nestedQuestionContainer').hide();
    }
}

The .questionAlternative is two radiobuttons, yes or no.

When I change to No, I get the following error:

Error: Syntax error, unrecognized expression: #
throw new Error( "Syntax error, unrecognized expression: " + msg );

I can't understand why. If nestedQuestion is empty, It should not go into the If-statement. But It seems like It doing It anyway?

1
  • 3
    "" != null is true Commented Oct 31, 2016 at 14:52

1 Answer 1

6

Your condition is wrong. It says "if nestedQuestion is not null OR nestedQuestion is not ""..." But "" is not null, so that condition is true for "".

Change it to and (&&):

if(nestedQuestion != null && nestedQuestion != "") {

or just use the fact that both null and "" are falsy:

if(nestedQuestion) {

..and actually, attr will never give you null anyway. It will only give you a string (possibly "") or undefined. (But undefined == null is true, so...)

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, did not know that "" was not null. Thank you.
@Bryan: :-) It's one of those things, they're both falsy, but they're not == to each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.