Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With jQuery, how do I select an element with a meta-character in its name?

Let's say I have custom HTML tags like so:

<fb:est hours="5">5 Hours</fb:est>
<fb:act hours="4">4 Hours</fb:act>

How do I select the fb:est elements?

Doing var e = $('fb:est') does not work. And var e = $('fb\:est') doesn't work either.

like image 543
Rob Sobers Avatar asked Jan 22 '23 07:01

Rob Sobers


1 Answers

From the jQuery docs:

If you wish to use any of the meta-characters (#;&,.+*~':"!^$=>|/@ ) as a literal part of a name, you must escape the character with two backslashes: \. For example, if you have an an input with name="names[]", you can use the selector $("input[name=names\[\]]").

So, this is the way to do it:

var e = $('fb\\:est')

like image 134
Rob Sobers Avatar answered Feb 01 '23 22:02

Rob Sobers