Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,271 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.