0
$('#a.b') // doesn't work

document.getElementById('a.b') // works

$('[id="a.b"]') // works

Can anyone explain why $('#a.b') doesn't work.

Raj
  • 570
  • 5
  • 15

3 Answers3

1

Do not use meta characters in your Id's while using Jquery.

. is a meta character and selector see them as regex. You need to escape to make it work. For ex \\.

In this case -

$('#a\\.b')

Javascript getElementById also works because, for it, it's just a String.

When you giving as id="a.b" it checks for a value and won't get treated as meta character.

Raj
  • 570
  • 5
  • 15
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

jQuery thinks it is a class. So, you write it using escape sequence as -

$('#a\\.b')
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
0

You should use the escaping operator as follows:

$('#a\\.b')

jQuery does not allow meta-characters as a literal part of a name, and those must be escaped with two backslashes \\.

graphy
  • 11
  • 3