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

by Yang Yang on April 16, 2009

Share This Article:
Subscribe to Kavoir: blog feed

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.

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

{ 1 comment… read it below or add one }

INDK October 4, 2010 at 8:43 pm

Thanks Buddy that was helpful

Reply

Leave a Comment

Previous post:

Next post: