Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Uncaught Error: Syntax error, unrecognized expression

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

In my HTML, I have:

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

In the above code, I have one <div> with an id of 2013-10-23, and when getting that id it is throwing this syntax error:

Uncaught Error: Syntax error, unrecognized expression: "#2013-10-23"
like image 767
Priya Avatar asked Oct 03 '13 12:10

Priya


2 Answers

try

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

your solution is passing the double quotes as part of the string.

like image 88
webduvet Avatar answered Oct 24 '22 15:10

webduvet


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 */ }
like image 18
Martijn Avatar answered Oct 24 '22 16:10

Martijn