Welcome to the Treehouse Community
Looking to learn something new?
There's no documentation on Element.attribute
Upon researching on how to retrieve the value of an attribute attached to an element, I can't necessarily find
element.attribute documentation , however,
element.attributes is returned in a search.
Thus I have to ask, is this property even used in production? I can't find the property under the Elements interface in MDN. Are
getAttribute() more commonly used?
Steven Parker220,937 Points
No, I don't think elements have an "attribute" property.
But some attributes can be accessed just by name. For example, if you had an element referenced by a variable named "test", you could access its id attribute with: "
test.id". Some attributes can be accessed by other names, like you could see the class settings with "
If you're not sure if an attribute has direct access, you can use attributes or the getAttribute() method. For example, these are all ways to get the value of the title attribute of an element referenced by the variable "test":
titleValue = test.title; // note: this only works for a few attributes titleValue = test["title"]; // same here titleValue = test.attributes.title.value; titleValue = test.getAttribute("title");
And here is the MDN documentation for the "attributes" property.