0

I have an element with class "4.0.3" how can I search for it?

ff 12.x & latest chrome returns nothing for

console.log($('.4\.0\.3').length);
console.log($(".4.0.3").length);

On the other hand .hasClass() correctly returns if the element got a class that contains .

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Radek
  • 13,813
  • 52
  • 161
  • 255

2 Answers2

7

Escape it with double back-slash

$('.4\\.0\\.3')

http://api.jquery.com/category/selectors/

If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, you must escape the character with two backslashes: \\

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • @zerkms - RE: your deleted comment, RE: my deleted answer... but the dot character (`.`) is `U+002E`, which is, of course, less than `U+00A0`. So, not allowed, right? I mean, I see that [in practice it works... sort of](http://jsfiddle.net/VcWfb/), but it shouldn't according to CSS naming rules, right? Or am I missing something? – gilly3 Jun 28 '12 at 01:07
  • @gilly3: Yep, In some unknown reason I've read `A0` as `0A` :-S So you aren't missing something, I just need glasses :-) – zerkms Jun 28 '12 at 01:13
  • I can get dots to work, but I can't get a class name starting with a number to work without the help of jQuery. `document.querySelectorAll(".1")` throws a syntax error. jQuery must be circumventing `document.querySelectorAll()` in this case. – gilly3 Jun 28 '12 at 01:17
  • @gilly3: jQuery does try `document.querySelectorAll()`. When it receives an exception, then it'll fall back to Sizzle automatically. – BoltClock Jun 28 '12 at 01:27
2

As zerkms suggests, you can escape it but you need double backslashes:

$('.4\\.0\\.3')

Or for a less hacky solution, use an attribute selector instead:

$('[class~="4.0.3"]')
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356