Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div with the id="(name)" can I use this or not? [duplicate]

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.

like image 821
Vaishali Modi Avatar asked Sep 10 '13 12:09

Vaishali Modi


People also ask

Can 2 divs have the same ID?

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.

Can a div element have an ID?

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.

What happens if you have multiple elements with the same ID?

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.

Can Id be duplicated in HTML?

The ID attribute uniquely identifies elements on a page. It does not make sense to duplicate an ID.


3 Answers

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

like image 71
mplungjan Avatar answered Sep 30 '22 01:09

mplungjan


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/

like image 42
Jakub Matczak Avatar answered Sep 30 '22 02:09

Jakub Matczak


can you please check this jsfiddle

$(document).ready(function() {
$("#RSI\\(3\\,4\\)").html("some text");
});

Working Fine

like image 21
Shakti Patel Avatar answered Sep 30 '22 02:09

Shakti Patel