36
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"
Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Priya
  • 1,453
  • 4
  • 29
  • 55

9 Answers9

43

try

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

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

webduvet
  • 4,220
  • 2
  • 28
  • 39
19

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 */ }
Martijn
  • 15,791
  • 4
  • 36
  • 68
8

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.

OneThreeSeven
  • 422
  • 5
  • 13
7

Try using:

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

This will remove the extra quotes you were using.

Derlin
  • 9,572
  • 2
  • 32
  • 53
Sunil Verma
  • 2,490
  • 1
  • 14
  • 15
6

Try this (ES5)

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

ES6

console.log($(`#${d}`));
RDK
  • 4,540
  • 2
  • 20
  • 29
3

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 the issue was so I could troubleshoot and fix it.

JSG
  • 390
  • 1
  • 4
  • 13
  • 1
    Thank you for this. It does help us to understand how the structure of the JQUERY error message points to the specific problem location. And indeed - this was the error I was having - I was missing a # id when trying to concatenate values for AJAX i.e. $("#"+Data_G).serialize() – TV-C-1-5 Mar 14 '22 at 00:41
0

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);
Pang
  • 9,564
  • 146
  • 81
  • 122
Fendi Septiawan
  • 453
  • 5
  • 6
0

For some people coming here, you might have a special character in your id attribute, so jQuery can't read it correctly.

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Check this answer for more details: What are valid values for the id attribute in HTML?

Teo Mihaila
  • 134
  • 1
  • 2
  • 18
0

I had a selector with space ('#test .test').

Example of my error:

var href = "javascript:scrollto('"+key+"')";

For me this helped: encodeURIComponent(value)

var href = "javascript:scrollto('"+encodeURIComponent(key)+"')";
Thomas
  • 76
  • 8