Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Making Changes to the DOM Changing Element Attributes

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 setAttribute() & getAttribute() more commonly used?

1 Answer

Steven Parker
Steven Parker
220,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 "test.className".

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.