JavaScriptDOM and events

Element.setAttribute()

Sets the value of an element’s HTML attribute, or adds a new attribute.

Returns
undefined
Changes the original
Yes
On this page

Example

#
JavaScript
const link = document.querySelector('#next')

link.setAttribute('href', '/lesson-2')
link.setAttribute('title', 'Go to lesson 2')

console.log(link.getAttribute('href'))
console.log(link.getAttribute('title'))
console.log(link.getAttribute('target'))
console.log(link.hasAttribute('title'))
Console
/lesson-2
Go to lesson 2
null
true

Definition and usage

#

The setAttribute(name, value) method sets an element’s attribute. If the attribute already exists, its value gets overwritten, and if not, it gets added. The value is always converted to a string.

It’s accompanied by getAttribute() (reads it, returns null when the attribute is missing), hasAttribute(), removeAttribute(), and toggleAttribute(). These are especially useful for ARIA attributes, like aria-expanded, and for attributes that have no matching element property.

Many attributes also have a matching property, e.g. element.id, link.href, or input.value. They don’t always hold the same thing: getAttribute("href") returns the text typed into the HTML, while link.href returns the full address. A form field’s value attribute is its initial value, while the value property is the text currently typed by the user.

Boolean attributes, such as disabled or hidden, work through mere presence. Even setAttribute("disabled", "false") disables the button. To enable it, remove the attribute with removeAttribute(), or set the button.disabled = false property.

Syntax

#
Syntax
element.setAttribute(name, value)

element.getAttribute(name)
element.hasAttribute(name)
element.removeAttribute(name)
element.toggleAttribute(name)

Parameters

#
  • name

    string

    The attribute name, e.g. "href", "alt", or "aria-expanded".
  • value

    string

    The new attribute value. Other types get converted to a string.

More examples

#
Boolean attributes
JavaScript
const send = document.querySelector('#send')

send.setAttribute('disabled', 'false')
console.log(send.disabled)

send.removeAttribute('disabled')
console.log(send.disabled)

send.toggleAttribute('disabled')
console.log(send.hasAttribute('disabled'))
Console
true
false
true
The attribute versus the value property
JavaScript
const nick = document.querySelector('#nick')

nick.value = 'Emma'

console.log(nick.value)
console.log(nick.getAttribute('value'))
Console
Emma
Guest
ARIA in a dropdown menu
JavaScript
const menuButton = document.querySelector('#menu-btn')

menuButton.addEventListener('click', () => {
  const expanded = menuButton.getAttribute('aria-expanded') === 'true'
  menuButton.setAttribute('aria-expanded', String(!expanded))
  console.log('aria-expanded:', menuButton.getAttribute('aria-expanded'))
})

menuButton.click()
menuButton.click()
Console
aria-expanded: true
aria-expanded: false

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • For data-* attributes, it’s more convenient to use the dataset property.
  • You can only turn off a boolean attribute with removeAttribute(). The value "false" still means the attribute is present.
  • Use classList for classes and style for styles, instead of overwriting the whole class and style attributes.

Related entries

#

Spotted a mistake or missing an example? Write to us.