I understand that an id must be unique within an HTML/XHTML page.
My question is, for a given element, can I assign multiple ids to it?
<div id="nested_element_123 task_123"></div>
I realize I have an easy solution with simply using a class. I'm just curious about using ids in this manner.
Contrary to what everyone else said, the correct answer is YES
The Selectors spec is very clear about this:
Edit
Just to clarify: Yes, an XHTML element can have multiple ids, e.g.
but assigning multiple ids to the same
id
attribute using a space-separated list is not possible.http://www.w3.org/TR/REC-html40/struct/global.html#h-7.5.2
and
So "id" must be unique and can't contain a space.
No. Every DOM element, if it has an id, has a single, unique id. You could approximate it using something like:
and then use navigation to get what you really want.
If you are just looking to apply styles, class names are better.
I know this is a year old but I was curious about this myself and I'm sure others will find their way here. The simple answer is no, as others have said before me. An element can't have more than one ID and an ID can't be used more than once in a page. Try it out and you'll see how well it doesn't work.
In reponse to tvanfosson's answer regarding the use of the same ID in two different elements. As far as I'm aware an ID can only be used once in a page regardless of whether it's attached to a different tag.
By definition, an element needing an ID should be unique but if you need two ID's then it's not really unique and needs a class instead.
No. From the XHTML 1.0 Spec
I don´t think you can have two Id´s but it should be possible. Using the same id twice is a different case... like two people using the same passport. However one person could have multiple passports... Came looking for this since I have a situation where a single employee can have several functions. Say "sysadm" and "team coordinator" having the id="sysadm teamcoordinator" would let me reference them from other pages so that employees.html#sysadm and employees.html#teamcoordinator would lead to the same place... One day somebody else might take over the team coordinator function while the sysadm remains the sysadm... then I only have to change the ids on the employees.html page ... but like I said - it doesn´t work :(