Why does getElementById not work on elements inside the document element? [duplicate]

Container IDs should be unique, so there’s no reason to find an object by ID within another container. This is why you only need document.getElementById to access any element by its ID, whereas when you are searching by class or tag name, you might want to only search within a specific container, which is why you can do x.getElementsByClassName etc.

Leave a Comment