Document.getElementById () returns an element with a name equal to the specified identifier

I already mentioned in this SO about the funny behavior for IE6 / 7 (and some versions of Opera) that document.getElementById can find an element whose name is not an id attribute, so

 function f() { document.getElementById("a1").value = ...; } ... <input name="a1" ...></input> 

really works in these versions.

Web search I found this Chris Bloom bug report in which a user named Milo van der Ley points to the following (as indicated by him in this w3c spec ):

In their defense: "The identifiers id and name have the same namespace."

What does it mean that the id and name attributes have the same namespace? Why would this condition be sufficient for IE6 / 7 / Opera to implement this behavior in its JS engine?

+4
source share
1 answer

The term "same namespace" means that names and identifiers are not completely separate. You can use the same name and id on one specific object, but you cannot use name="foo" for one object and id="foo" for another object. This creates a conflict.

This is how these browsers decided to implement things. There is also a global variable for each element with an identifier containing the dom element. This is how they implemented things. It was not standard, it's not about what they do in more modern browsers (with the exception of some backward compatibility).

Use id values ​​for any DOM elements you want to get. Use the name values ​​to identify the server in published forms.

Your code will not have conflicts between names and identifiers if you do not use id for one object and the same name for another object, and, as a rule, there is no problem providing a specific element with the same name and id .

+3
source

Source: https://habr.com/ru/post/1444969/


All Articles