Web APIs

Element.toggleAttribute()

Try the free JavaScript compiler

Toggle an attribute by presence or enforce its presence using a Boolean force argument. Example console output: true false.

Formal Syntax & Structure

javascript syntaxjavascript
element.toggleAttribute(name[, force])

Parameters & Attributes

NameTypeDescription
namestringAttribute name.
forcebooleanDesired presence when supplied.

Return Value

boolean

Whether the attribute is present after the call.

Working Example

Example Snippetjavascript
const p=document.createElement("p"); console.log(p.toggleAttribute("hidden",true),p.toggleAttribute("hidden"));
JavaScript Reference