icepopo
icepopo

Reputation: 1637

getElementById trouble

html: [...]<div id="test" name="testvalue"></div>[...]

js[...]alert(document.getElementById("test").name);[...]

Why I get 'undefined' instead of 'testvalue'?

Upvotes: 2

Views: 127

Answers (1)

user113716
user113716

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

Related Questions