JavaScriptDOM and events

Element.toggleAttribute()

Adds an attribute when it’s missing, or removes it when it’s present. Works best for boolean attributes, such as hidden or disabled.

Returns
true if the attribute is present after the call, otherwise false.
Changes the original
Yes
On this page

Example

#
JavaScript
const toggle = document.querySelector('#toggle')
const explanation = document.querySelector('#explanation')

toggle.addEventListener('click', () => {
  const hidden = explanation.toggleAttribute('hidden')
  toggle.textContent = hidden ? 'Show explanation' : 'Hide explanation'
  console.log('hidden:', hidden)
})

toggle.click()
toggle.click()
Console
hidden: false
hidden: true

The first click removes the hidden attribute and shows the paragraph, the second adds it back. Click the button in the preview to keep toggling the explanation.

Definition and usage

#

The toggleAttribute() method toggles an attribute’s presence: if the element doesn’t have it, it adds it with an empty value, and if it does, it removes it. It returns true when the attribute is present after the call, and false when it isn’t.

It’s most useful for boolean attributes, which work through mere presence, e.g. hidden, disabled, required, readonly, or open. The value doesn’t matter for them, only whether the attribute is written at all. It also works with custom attributes, e.g. data-active, which you can use in CSS selectors.

The second argument, force, forces the outcome: true always adds the attribute (or leaves an existing one), and false always removes it. That lets you set the state based on a condition with a single call, without writing an if with setAttribute() and removeAttribute(). It works similarly to classList.toggle(), just for attributes.

Syntax

#
Syntax
element.toggleAttribute(name)
element.toggleAttribute(name, force)

Parameters

#
  • name

    string

    The attribute name, e.g. "hidden" or "disabled".
  • force

    optional, boolean

    true always adds the attribute, and false always removes it.

More examples

#
Forcing a state with the second argument
JavaScript
const nick = document.querySelector('#nick')
const join = document.querySelector('#join')

function updateButton() {
  join.toggleAttribute('disabled', nick.value.trim().length < 3)
  console.log('Button disabled:', join.disabled)
}

nick.addEventListener('input', updateButton)
updateButton()

nick.value = 'Amy'
nick.dispatchEvent(new Event('input'))
Console
Button disabled: true
Button disabled: false

The condition decides the button’s state without any if. Type something into the field in the preview, and the button will enable or disable.

The attribute’s value and the method’s result
JavaScript
const faq = document.querySelector('#faq')

console.log(faq.toggleAttribute('open'))
console.log(JSON.stringify(faq.getAttribute('open')))
console.log(faq.open)
console.log(faq.toggleAttribute('open', true))
console.log(faq.toggleAttribute('open', false), faq.open)
Console
true
""
true
true
false false

The added attribute has an empty value, since boolean attributes only care about their presence. With force set to true, an attribute that already exists is simply left alone.

Browser support

#

Widely available

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

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • For boolean attributes (hidden, disabled, required), use toggleAttribute() or removeAttribute(). setAttribute("hidden", "false") still hides the element.
  • The second argument turns toggling into setting: toggleAttribute("disabled", !formValid) immediately applies the right state.
  • For many boolean attributes, element properties work just as well, e.g. button.disabled = true or panel.hidden = false.

Related entries

#

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