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?
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: \.
Working jsFiddle Demo
Use attribute selectors1:
var site = 'someSite.com';
var variable = $('[id="site_' + site + '"]');
// do something
variable.css('background', 'yellow');
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With