jQuery: Selecting elements with uncommon / special characters in ID or class name

HTML generated by some CMS or frameworks include elements with rather uncommon characters in ID or class names. For example, some may have special characters such as ‘.’ or ‘[..]’ in the ID or Class. To work around this, a selector in jQuery should be written this way:

$("$some.id") // won't work for ID: some.id $("$some\\.id") // works for ID: some.id

With brackets in ID:

$("$some[id]") // won't work for ID: some[id] $("$some\\[id\\]") // works for ID: some[id]

You get the idea. Just add double backslashes \\ before any of the special characters.

1 thought on “jQuery: Selecting elements with uncommon / special characters in ID or class name”

Comments are closed.

Scroll to Top