liveBook · Manning

Attribute

As explained in Vanilla Web

A JavaScript property is a named value associated with an object.

An HTML attribute is a named value on an element’s markup. Attributes give the browser information about an element and can also be read by CSS and JavaScript. They are not the same as JavaScript properties: attributes are strings on the DOM element, while properties live on its JavaScript object and may have other types. section 3.4.3 section 6.2.8

Choose attributes according to what the value controls:

  • Use native semantic attributes when they fit. For example, `lang` identifies content language, `alt` supplies image text, and the presence of `disabled` makes an interactive element non-interactive. Prefer these to recreating the behavior with CSS or JavaScript. section 3.4.3
  • Use direct attributes such as `variant` or `size` for a custom element’s core configuration. Reserve `data-*` for application-specific metadata; `dataset` exposes those values as strings in JavaScript. section 7.9.1 section 6.2.8
  • Use properties for complex values such as objects or arrays, and CSS custom properties for styling choices. Attributes suit simple values that can be expressed as strings. section 7.9
const input = document.createElement("input");
input.setAttribute("value", "A");
document.body.append(input);
input.value = "B";
console.log(input.getAttribute("value"));  // "A"
console.log(input.value);                  // "B"

The input’s `value` attribute remains `"A"` after its JavaScript `value` property becomes `"B"`. For form controls, an attribute may retain the initial value while the property tracks changing state. section 6.2.8

Read or write an attribute with `getAttribute()` and `setAttribute()`. Do not assume a same-named property exists or stays synchronized: some attributes are reflected, while others are not. Boolean attributes such as `disabled` express their state by presence or absence, rather than by a JavaScript boolean stored in markup. section 6.2.8

A small custom-element contract

`<star-rating value="3">` supplies a simple value in HTML. The element can read it with `getAttribute('value')`; a JavaScript property can offer a separate typed interface if needed. If consumers change a property, its setter must also trigger any component update that change requires. Document public attributes alongside properties and events. section 7.9 section 7.9.4 section 9.9.5

How Vanilla Web covers it

How deep it goes
Distinguishes markup attributes from JavaScript properties, including string values, reflection, form-state divergence, and DOM access methods.
Examples
Uses an input whose attribute and property diverge, plus a star-rating element configured with a value attribute.
What it assumes
Assumes basic HTML and JavaScript element access while explaining the attribute/property distinction explicitly.
What you can do after
Build it from scratch: choose and document custom-element attributes, read them through DOM APIs, and avoid assuming property synchronization.
How it connects
Connects attributes to DOM properties, accessibility semantics, dataset, CSS custom properties, and Web Components.

Also explained in

Compare how 32 books explain attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.