setAttribute()
The DOM method that sets an attribute on an element, with getAttribute() as its partner for reading one.
In more detail
element.setAttribute('aria-expanded', 'true') writes the attribute into the DOM, where CSS selectors, assistive technology and MutationObserver can all see it. Attribute values are always strings.
Related terms
- HTML attribute — A name and value written inside a start tag, such as class="card" or hidden, that configures an element.
- data attribute — A custom HTML attribute whose name starts with data-, such as data-theme="dark", used to store extra information on an element.
- ARIA states and properties — The aria-* attributes that describe an element's current condition or relationships, such as aria-expanded="true" or aria-controls="panel-1".
Taught in
- HTML Parsing, Tokenization And The Document Object Model (DOM) — Frontend Architecture & Web Platform
- Accessibility Tree, ARIA Roles, States And Live Regions — Frontend Architecture & Web Platform
- Modern DOM Querying: querySelector, closest And matches — Frontend Architecture & Web Platform
- MutationObserver: Reactive DOM Watching Without Polling — Frontend Architecture & Web Platform