JavaScript Reference ยท DOM: Document and Elements
Element.getAttribute() and setAttribute()
Reference for getAttribute(), setAttribute(), removeAttribute(), hasAttribute() and toggleAttribute(): read and write HTML attributes and how they differ from properties.
An HTML attribute is the text you write in a start tag, such as href="/docs" or disabled. The DOM stores those attributes on each element and gives you methods to read and change them: getAttribute() returns the stored string (or null), and setAttribute() creates or changes one.
Attributes are not the same thing as JavaScript properties. Many properties reflect an attribute (img.alt and alt), but some differ in type or meaning (input.value, a.href, checkbox.checked), so choose deliberately which one you want.
link.getAttribute("href"); link.setAttribute("target", "_blank"); link.removeAttribute("target");
Try it
After the user (or a script) changes the value, the value attribute only holds the default, and the property holds the current state. Use properties for live state and attributes for the markup.
Syntax
element.getAttribute(name) element.setAttribute(name, value) element.removeAttribute(name) element.hasAttribute(name) element.toggleAttribute(name, force) element.getAttributeNames()
| Method | Description |
|---|---|
getAttribute(name) |
Returns the attribute value as a string, or null if it is not set. A present boolean attribute such as disabled gives "". |
setAttribute(name, value) |
Sets the attribute, creating it if needed. The value is converted to a string. Returns undefined. |
removeAttribute(name) |
Deletes the attribute. Does nothing if it is missing. Returns undefined. |
hasAttribute(name) |
Returns true if the attribute exists, even with an empty value. |
toggleAttribute(name, force) |
Adds the attribute (with an empty value) if absent, removes it if present. With force, true only adds and false only removes. Returns true if the attribute is present afterwards. |
getAttributeNames() |
Returns an array with the names of all attributes. |
Exceptions: InvalidCharacterError (DOMException) if name is not a valid attribute name, for example one containing a space.
Usage notes
- Names are case-insensitive for HTML elements. In an HTML document the name is lowercased first, so
setAttribute("DATA-X", ...)createsdata-x. - Values are always strings.
setAttribute("hidden", false)still sets the attribute to the text"false", and a boolean attribute is true whenever it is present. To turn one off, callremoveAttribute()ortoggleAttribute(name, false). getAttribute()returns the source value. Forhref, the attribute is the text as written ("/docs"), while thelink.hrefproperty is the full resolved URL.- Properties are often better. Use
el.id,el.title,input.value,input.checkedandbutton.disabledfor typed, live access. Use attributes for customdata-*andaria-*values, or when you need the exact markup. - ARIA.
aria-*attributes are written withsetAttribute("aria-expanded", "true"). They are strings, so pass"true"or"false"explicitly. nullcheck. Compare againstnull, not falsy, because an existing attribute with value""is meaningful.- Event handlers. Do not set
onclickviasetAttribute()with code strings. UseaddEventListener().
Browser support
Supported in every browser. toggleAttribute() and getAttributeNames() are newer additions, but are available in all current browsers.
Related
HTMLElement.datasetโdata-*attributes as an objectElement.classListโ theclassattribute as a listHTMLElement.styleโ thestyleattribute as an object- Lesson: Selecting elements
- Lesson: The DOM