5

Possible Duplicate:
How do I get jQuery to select elements with a . (period) in their ID?

I tried to run the following code:

$('#info-mail.ru .domain-info').toggle();

example here

And #info-mail.ru as I understood interpreted as id="info-mail" and class="ru", but I have the following structure:

<div id="info-mail.ru">
    <p class="domain-info">
        Some cool info
        Some cool info
        Some cool info
        Some cool info
    </p>
</div>

How can I shield "." char in selector statement? Or the only way is to replace all "." with "_" (for example)?

TIA!

Community
  • 1
  • 1
Dmitry Belaventsev
  • 6,347
  • 12
  • 52
  • 75

2 Answers2

7

Escape . with a double backslash, one for the literal and the other for jQuery:

$('#info-mail\\.ru .domain-info').toggle();

See jQuery FAQ for details.

Saul
  • 17,973
  • 8
  • 64
  • 88
  • 1
    Remove last \\ : $('#info-mail\\.ru .domain-info').toggle(); The last one is intended to be a class selector. – Robin Castlin Sep 30 '11 at 09:04
  • thx! work perfect! could you explain - where this "shield" syntax is described (i.e. "\\" )? – Dmitry Belaventsev Sep 30 '11 at 09:06
  • @dizpers - They're not called shield characters but **escape characters** – m.edmondson Sep 30 '11 at 09:09
  • @dizpers - See [jQuery FAQ](http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_select_an_element_by_an_ID_that_has_characters_used_in_CSS_notation.3F). It boils down to expressing \ with a literal which jQuery interprets as a command to escape the next character, in this case `.` – Saul Sep 30 '11 at 09:13
1

I believe you need to use two backslashes before the .

So the selector would be $('#info-mail\\.ru .domain-info').toggle();

m.edmondson
  • 30,382
  • 27
  • 123
  • 206