Can I use id with "("
and ")"
in jquery mobile ?
for example:
<div id="RSI(3,4)">
</div>
I have tried that id and want to access that element in js.
$("#RSI(3,4)").html("some text");
but it didn't worked.
The HTML id attribute is used to specify a unique id for an HTML element. You cannot have more than one element with the same id in an HTML document.
The id attribute assigns an identifier to the <div> element. The id allows JavaScript to easily access the <div> element. It is also used to point to a specific id selector in a style sheet.
The id must be unique. There can be only one element in the document with the given id . If there are multiple elements with the same id , then the behavior of methods that use it is unpredictable, e.g. document. getElementById may return any of such elements at random.
The ID attribute uniquely identifies elements on a page. It does not make sense to duplicate an ID.
Escape the special chars
$("#RSI\\(3\\,4\\)").html("some text");
http://api.jquery.com/category/selectors/
To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. For example, an element with id="foo.bar", can use the selector $("#foo\\.bar").
That said, about the validity of using special characters in an ID, some validators would complain and I do certainly NOT recommend using any special chars in an ID nor use leading digits just because you can according to the latest html specs. Make your life easier and use only a-zA-Z0-9 and if needed, underscore
W3C Validator says that this html code
<html>
<head>
<title>TITLE</title>
</head>
<body>
<div id="RSI(3,4)"></div>
</body>
</html>
is valid for html5 but not for html4.01 Strict, for which (
is invalid character in id
attribute.
http://validator.w3.org/check
In jQuery you have to escape them with \\
http://api.jquery.com/category/selectors/
can you please check this jsfiddle
$(document).ready(function() {
$("#RSI\\(3\\,4\\)").html("some text");
});
Working Fine
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