Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
link.getAttribute("href");
link.setAttribute("target", "_blank");
link.removeAttribute("target");

Try it

Attribute methods
Attribute versus property

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

JavaScript
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", ...) creates data-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, call removeAttribute() or toggleAttribute(name, false).
  • getAttribute() returns the source value. For href, the attribute is the text as written ("/docs"), while the link.href property is the full resolved URL.
  • Properties are often better. Use el.id, el.title, input.value, input.checked and button.disabled for typed, live access. Use attributes for custom data-* and aria-* values, or when you need the exact markup.
  • ARIA. aria-* attributes are written with setAttribute("aria-expanded", "true"). They are strings, so pass "true" or "false" explicitly.
  • null check. Compare against null, not falsy, because an existing attribute with value "" is meaningful.
  • Event handlers. Do not set onclick via setAttribute() with code strings. Use addEventListener().

Browser support

Supported in every browser. toggleAttribute() and getAttributeNames() are newer additions, but are available in all current browsers.