One reason for not having multiple instances of an ID that was not mentioned is that it's not allowed according to HTML specifications. An ID should only appear on a single element within a document. The result of violating this restriction is undefined and browser-dependent.
I think that in many cases, a data- element might be an appropriate substitute. I've started using data- elements to provide RESTful IDs in lists. It just seems awfully neat and tidy to include an ID on each
<li> element.
For example, I have a list of Places. I add a
data-place-id element to each
<li>. This has great utility. For example, I have a button in each list item in that needs to make an Ajax request (to a local Rhodes server on a mobile device) using Javascript.
If you have a click handler, for example, for any descendant of the
<li> that passes "this" to a function, then the function can easily get the ID. For example, this click handler:
- peepr.show_muc = function(obj) {
- $.get("/app/Place/chat/" + $(obj).closest('li.place-item', $.mobile.activePage).attr("data-place-id"));
- return false;
- }
.closest gets me the nearest ancestor that matches the given selector.
(I don't use the response. The server code forces a load into a separate webview on a different native tab.)
Sure, I could have just provided an href in the link, and accessed the
href from the function. But there is other utility in having the ID
present on the
<li> element. For example you might want to do
continuous scrolling and you could use th IDs to manage trimming and
adding items to the head/tail of the list as the user scrolls.
data- attributes don't have any restriction on multiple occurrences in a document.