Bryan Bryan - 2 months ago 15
Javascript Question

Error: Syntax error, unrecognized expression: # jquery

I have the following code:

$('.questionAlternative').change(function() {
var nestedQuestion = $(this).attr('nestedQuestion');
if(nestedQuestion != null || nestedQuestion != "") {
} else {

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?


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...)