Contents
Can you return multiple elements with the same ID?
If you have several elements with the same ID, your HTML is not valid. So, document.getElementById should only ever return one element. You can’t make it return multiple elements. There are a couple of related functions that will return a list of elements: getElementsByName or getElementsByClassName that may be more suited to your requirements.
Can a class be used to replace multiple IDs?
Yes, multiple IDs can, in practice, be replaced by using classes. However, classes are intended for applying styles, not identifying elements, making the scope of names much wider and therefore likely to overlap.
Can you use more than one unique ID in HTML?
This happens because you are not allowed to use more than one of a unique ID per page. Everytime you use an ID, it should be uniquely represented in your HTML. If you want to be able to target multiple elements that are grouped or belong to a group, such as “toy” or “sandbox” then you should give them a class. e.g.
How to get the ID of an element?
The id is supposed to be unique, use the attribute “name” and “getelementsbyname” instead, and you’ll have your array. As others have stated, you shouldn’t have the same ID more than once in your HTML, however… elements with an ID are attached to the document object and to window on Internet Explorer. Refer to:
When do you use a long, string ID instead of?
On the non-sequential nature of the IDs: it means they don’t need a synchronized counter between all the servers that assign IDs to videos. They can just generate a random number, check if it’s already in use, and go from there. They could even assign each server a block of IDs to pick from and eliminate the duplication checking.
Can you have more than one ID in HTML?
As others have stated, you shouldn’t have the same ID more than once in your HTML, however… elements with an ID are attached to the document object and to window on Internet Explorer.
Can a document return more than one element?
The HTML spec requires the id attribute to be unique in a page: If you have several elements with the same ID, your HTML is not valid. So, document.getElementById should only ever return one element. You can’t make it return multiple elements.