In javascript, I would like to get the name attribute of an element.
- The Mozilla Developper Network says we can use
e.nameore.elementName(the latter gives undefined in Chrome for me) - This StackOverflow answer says to use
e.attributes["name"].value - I guess if I tried, I’d find a few more ways.
Which way is standard-compilant ?
Which way is most compatible ?
Where can I get a list of how to access each attribute (for example, id can be accessed directly by e.id, and I’m pretty sure it’s a standard-compliant way to access it) ?
To some degree it depends on the element type. The
nameattribute isn’t supported in all element types, so it would depend on if you’re using it in a valid way.To get the value in the broadest way, use
.getAttribute().If you’re using
nameon elements that support that attribute, then I’d just use the.nameproperty.