[jquery] JQUERY: Uncaught Error: Syntax error, unrecognized expression

console.log($('"#'+d+'"'));

in html i have

<div id="2013-10-23">
    <h1>5</h1>
    <p>eeeeeeeeeeee</p>
</div>

It throws error:

Uncaught Error: Syntax error, unrecognized expression: "#2013-10-23"

In above code i have one div with id="2013-10-23" and when getting that id in response it is throwing syntax error

This question is related to jquery

The answer is


Try using:

console.log($("#"+d));

This will remove the extra quotes you were using.


I had to look a little more to solve my problem but what solved it was finding where the error was. Here It shows how to do that in Jquery's error dump.

In my case id was empty and $("#" + id);; produces the error.

It was where I wasn't looking so that helped pinpoint where it was so I could troubleshoot and fix it.


If you're using jQuery 2.1.4 or above, try this:

$("#" + this.d);

Or, you can define var before using it. It makes your code simpler.

var d = this.d
$("#" + d);

Try this (ES5)

console.log($("#" +  d));

ES6

console.log($(`#${d}`));

This can also happen in safari if you try a selector with a missing ], for example

$('select[name="something"')

but interestingly, this same jquery selector with a missing bracket will work in chrome.


The "double quote" + 'single quote' combo is not needed

console.log( $('#'+d) ); // single quotes only
console.log( $("#"+d) ); // double quotes only

Your selector results like this, which is overkill with the quotes:

$('"#abc"') // -> it'll try to find  <div id='"#abc"'>

// In css, this would be the equivalent:
"#abc"{ /* Wrong */ } // instead of:
#abc{ /* Right */ }