Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select ID containing dot

Tags:

html

jquery

css

I have some nodes which may occasionally contain a dot in their ID attribute,

Example: <div id="site_someSite.com"></div>

Then I need to select this element ( I'm using jQuery ) and I do this:

variable = $('#site_'+user); user being 'someSite.com'

And I get nothing in the variable. I suppose this is due to the dot in the name which jQuery is accepting as a class. Anyone has any idea how I can work around this without not-using a dot?

like image 967
php_nub_qq Avatar asked Aug 01 '26 08:08

php_nub_qq


2 Answers

You can use it but escape it by backslashes,like $('#example\\.afterdottext');

As per docs

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \.

like image 128
Suresh Atta Avatar answered Aug 03 '26 23:08

Suresh Atta


Working jsFiddle Demo

Use attribute selectors1:

var site = 'someSite.com';
var variable = $('[id="site_' + site + '"]');

// do something
variable.css('background', 'yellow');

References:

  • 1 Attribute Equals Selector [name="value"] - jQuery API Documentation