Reputation: 1637
html: [...]<div id="test" name="testvalue"></div>[...]
js[...]alert(document.getElementById("test").name);[...]
Why I get 'undefined' instead of 'testvalue'?
Upvotes: 2
Views: 127
Reputation: 322582
Because name
is not a valid attribute for a <div>
element. As such, it doesn't map to the .name
property.
You could probably get it using the getAttribute()
method though:
alert(document.getElementById("test").getAttribute("name"));
Example: http://jsfiddle.net/BcYXL/
Upvotes: 9