getAttribute
Reads the raw attribute value.
Attributes live in HTML. Properties live on DOM objects. They often mirror each other, but they are not the same thing.
button.disabled = true;
button.setAttribute("aria-busy", "true");
const url = link.href;
DOM & HTML Interaction
An attribute is what appears in HTML, such as href, alt, aria-label or data-state. A property is the JavaScript object value, such as element.href or input.checked.
Some properties reflect attributes. Others represent live state that may differ from the original markup.
Boolean states such as disabled, checked and hidden are usually clearer as properties in JavaScript.
Reads the raw attribute value.
Writes an attribute string.
Reads or writes live DOM object state.
Use true or false for disabled, checked and hidden.
Examples
button.disabled = true;
button.setAttribute("aria-busy", "true");
status.textContent = "Saving...";
button.setAttribute("disabled", "false");
// The disabled attribute still disables the button.
Code patterns
These patterns are the DOM moves you use constantly: select, read, change, create, navigate and render.
Useful for custom attributes and source markup.
const label = button.getAttribute("aria-label");
ARIA attributes are string attributes.
button.setAttribute("aria-expanded", "true");
disabled is clearer as a property.
submitButton.disabled = formIsInvalid;
href property returns a resolved URL.
console.log(link.href);
Rules that matter
DOM APIs are powerful because they affect the live page. Use them with stable hooks, clear state and safe text updates.
disabled = false is clear and correct.
ARIA states are represented as attribute strings.
link.href may differ from the raw href attribute.
Use dataset for custom strings, not complex objects.
A styled disabled button should also be disabled in the DOM.
Attributes often communicate meaning to assistive technology.
Production thinking
Confusing attributes and properties creates UI states that look correct but behave incorrectly.
ARIA attributes, disabled state and labels must be updated together with visual changes.
Production components should have one clear source of truth for DOM state.
Attributes such as href, alt and structured-data scripts affect how content is discovered and understood.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain the DOM operation and its failure path, you are ready to use it in real UI code.
Senior audit upgrade
Prefer textContent, createElement, templates and replaceChildren for normal UI rendering. Reach for innerHTML only when the markup is controlled and the risk is understood.
const item = document.createElement("li");
item.textContent = userInput;
list.replaceChildren(item);