0

Possible Duplicate:
Difference between single quotes and double quotes in Javascript

I have a doc in the id attribute of a HTML tag:

I read the jQuery dot in ID selector? and http://api.jquery.com/category/selectors/ and I realize that I should add a 2 backslashes in order to escape the ".".

The problem is when I use $('#Q0\\.3'), it works.

But when I use $("#Q0\\.3"), jQuery does not find the element.

I thought that double-quotes and single-quote in jQuery stand for the same thing, was I wrong?

Thanks.

Community
  • 1
  • 1
Kewei Shang
  • 949
  • 3
  • 11
  • 27
  • Selector $('#Q0\.3') works with id="Q0.3" ? – Denis Ermolin Apr 23 '12 at 10:11
  • 1
    Works the same for both cases for me ... http://jsfiddle.net/gaby/dC7Db/ – Gabriele Petrioli Apr 23 '12 at 10:13
  • 2
    *"The problem is when I use `$('#Q0\\.3')`, it works. But when I use `$("#Q0\\.3")`, jQuery does not find the element."* No, that's impossible. By the time jQuery sees the value, it's a string. The kind of quotes you used to delimit the string makes no difference whatsoever. – T.J. Crowder Apr 23 '12 at 10:14
  • Unless you try `$('#Q0\\.3').remove()` first and then `$("#Q0\\.3")` :) – Gabriele Petrioli Apr 23 '12 at 10:15
  • @Gaby: This isn't a duplicate of that question, I wouldn't say. – T.J. Crowder Apr 23 '12 at 10:16
  • @T.J.Crowder, hmm.. the actual question is a duplicate, but the problem that prompted the question in not the same... how should we handle such a case ? I guess just pointing to the fact that the problem lies elsewhere .. ? – Gabriele Petrioli Apr 23 '12 at 10:34
  • @Gaby: In this case, it's not a real question (because the claim that the one works and the other doesn't is false), so that's what I went with. – T.J. Crowder Apr 23 '12 at 10:35
  • I checked once again, indeed, it was my fault. `$('#Q0\\.3')` and `$('#Q0\\.3')` work both. However, if I try `var id1 = '#Q0\\.3'; var id2 = '#Q0\\\\.3'; var idElement1 = $(id1); var idElement2 = $(id2);` idElement1 get the element while idElement2 doesn't. However, while I was debugging the code in firebug, id1 holds the value '#Q0\.3' and id2 holds the value '#Q0\\.3'. I guess the reason that jQuery needs 2 backslashes to escape a special character is that jQuery is expecting the '\\' to be escaped to '\'. What jQuery will be really using is the '\'. Am I correct? – Kewei Shang Apr 23 '12 at 11:48

0 Answers0