2

It is considered a good practice to have unique IDs of HTML elements. HTML standard specifies so:

https://www.w3schools.com/htmL/html_id.asp

You cannot have more than one element with the same id in an HTML document.

https://dom.spec.whatwg.org/#concept-id

Historically elements could have multiple identifiers e.g., by using the HTML id attribute and a DTD. This specification makes ID a concept of the DOM and allows for only one per element, given by an id attribute.

Why?

A page is going to work even with multiple IDs, it is not a hard requirement.

Note: I'm not arguing against the uniqueness of IDs, I just want to know what are the possible issues. I had to raise an issue for non-unique IDs, but the rest of the team didn't consider it to be very important.

6
  • 2
  • Thanks for the softwareengineering link. The SO one doesn't explain much, it just reiterates the ids need to be unique. – Mate Mrše Jun 16 at 13:37
  • @jonrsharpe Great links! I think the first one explain pretty well potential issues. – dzieciou Jun 16 at 18:49
  • "I had to raise an issue for non-unique IDs, but the rest of the team didn't consider it to be very important." -- So, does that mean they were using IDs like they were classes? – Lee Jensen Jun 16 at 20:13
  • That second link isn't referring to uniqueness of IDs, it's talking about multiple (hopefully unique) IDs assigned to a single element (one via the DOM, and another via DTD) – ernie Jun 16 at 20:41
2

My opinion is that technically it is hard enough to keep them really unique since part of a page could be a static template and another is generated dynamically.

If browsers would treat id uniqueness as a strict requirement it would worsen user experience for people encountering such problematic pages (should a browser fail to load a page and show big red dialog saying something "this page does not comply HTTP standard").

Historically browsers turn a blind eye to development flaws (like closing tags, not unique ids, inaccessible resources) just to improve user experience.

1
  • Having a non-unique ID is misleading. If a future developer is writing some JavaScript to do something with an ID, they expect the ID to be unique. If it isn't, it should be a class. – Wally Hartshorn Jun 22 at 19:13
1

I just want to know what are the possible issues. I had to raise an issue for non-unique IDs, but the rest of the team didn't consider it to be very important.

In some ways, I'd agree that it would be a low priority bug.

Since the devs here are essentially using IDs like Classes, then the issues I see are:

  • Browser JS console may contain extraneous warnings or errors (noise), which could make it harder to diagnose bugs when they occur.

  • Same with log files. It could create extra noise.

  • HTML won't pass linting or HTML validation tools.

  • If devs need to fix this pattern, then they'd have to fix it in the entire code base. If JavaScript/JQuery/CSS or other libraries are targeting these elements right now with the incorrect ID, then you'd have to fix those references to a Class reference. Depending on size of code base, it could be a large effort to fix. It could also require a full regression test. This could be why they don't want to fix it.

  • If there's test automation code that uses those incorrect IDs, well, the problem there is doing a .FindsByID will only find the first one as it expects only one. This would also be an issue with Xpath locators or other more complex CSS locators.

Ultimately, in order to get this fixed, I think the devs need training on proper use of IDs vs Class. And, you'd have to speak to them in language they'd understand, as in, how does this current way affect them? Right now, it seems they don't want to fix it because it creates too much work for them.

2
  • If they're using IDs like classes, presumably they should just use classes instead. That's what they're for. – Wally Hartshorn Jun 22 at 19:11
  • @WallyHartshorn True, but that's not the question. The question is about if there are side effects of using IDs instead of classes and why they aren't being fixed, which I've raised several points. – Lee Jensen Jun 23 at 21:40
0

The meaning of the word "identity" implies its uniqueness. If one says that two things can be identified by the same token, this token doesn't serve to distinguish these things.

enter image description here

1
  • 2
    Why is that a picture? It's entirely text content, why make it unindexable and inaccessible? – jonrsharpe Jun 16 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.