So I'm learning to manipulate the DOM and I noticed one interesting thing:
Let's say I want to set the name
attribute of an element by using the "." dot notation:
element.name = "someName";
console.log(document.getElementsByName("someName")[0]); // returns "undefined"??
However if I use the document.setAttribute()
method, it works fine:
element.setAttribute("name", "someName");
console.log(document.getElementsByName("someName")[0]); // returns the element like it should.
Not sure why the dot notation method doesn't work in the first case.
Why does this happen?
getElementsByName
– Geostatic