Can someone shed some light on the behavior of javascript getElementById() when there are elements with duplicate IDs in HTML DOM??
The getElementById() method returns an element with a specified value. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM. It is used almost every time you want to read or edit an HTML element.
Duplicate IDs are common validation errors that may break the accessibility of labels, e.g., form fields, table header cells. To fix the problem, change an ID value if it is used more than once to be sure each is unique.
The method getElementById can be called only on document object. It looks for the given id in the whole document.
No it is not possible. ID always refer to uniqueness.
While there is no standard behavior defined, typically it will return the first element found.
Demo: http://jsfiddle.net/ruNKK/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With