JavaScript getAttribute(): Read HTML attribute values
element.getAttribute(name) returns an HTML attribute as a string, or null if it is missing. Live values such as a checkbox’s current selection can differ from the markup. Read DOM properties such as checked or value for those states.
Syntax and return values
const link = document.getElementById("guide");
if (link !== null) {
const href = link.getAttribute("href");
console.log(href);
}
Call getAttribute() on an element. If getElementById() returns null, check the selector and when the code runs. The DOM, or Document Object Model, represents the page as objects. A missing element and a missing attribute on an existing element are separate conditions.
Separate empty attributes from missing ones
const title = element.getAttribute("title");
if (title === null) {
console.log("The title attribute is missing.");
} else if (title === "") {
console.log("The title attribute is empty.");
} else {
console.log(title);
}
A test like if (title) treats both an empty string and null as false. To ask only whether the attribute exists, use hasAttribute(). The data-* values are strings as well. dataset maps data-user-id to dataset.userId.
Compare href and checked in a complete example
Save the full code as get-attribute.html and open it. The link points to a real heading in the file. The checkbox starts checked and the script appears after the controls.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Read HTML attributes and state</title>
</head>
<body>
<h1>Read attributes</h1>
<p><a id="guide" href="#details" title="" data-count="3">Go to details</a></p>
<label><input id="agree" type="checkbox" checked> Acknowledge notice</label>
<pre id="output"></pre>
<h2 id="details">Details</h2>
<p>Uncheck and recheck the box to compare the results.</p>
<script>
const guide = document.getElementById("guide");
const agree = document.getElementById("agree");
const output = document.getElementById("output");
function render() {
output.textContent = [
`href attribute: ${guide.getAttribute("href")}`,
`href property: ${guide.href}`,
`title: ${JSON.stringify(guide.getAttribute("title"))}`,
`missing aria-label: ${JSON.stringify(guide.getAttribute("aria-label"))}`,
`data-count type: ${typeof guide.getAttribute("data-count")}`,
`checked attribute: ${JSON.stringify(agree.getAttribute("checked"))}`,
`current checked: ${agree.checked}`
].join("\n");
}
agree.addEventListener("change", render);
render();
</script>
</body>
</html>
Initially the href attribute is #details, title is "", the missing aria-label is null, and the count type is string. The href property resolves the address against the document’s base URL. Uncheck the box to see checked become false while the checked attribute stays empty. Remove title="", save and reload to compare with a missing attribute, then restore it.
Display attribute values as plain text
After HTML parsing, a character reference such as < in an attribute becomes the character <. Use textContent to display the value without parsing it as markup. Use setAttribute() and removeAttribute() to change markup attributes. Use the appropriate property for live form state.
const label = element.getAttribute("data-label");
output.textContent = label ?? "";
Convert an attribute to a count while preserving zero
On the example page, run the code in the Console. It clones the link and accepts only decimal integers zero or greater. It checks the trimmed string before applying Number() and Number.isSafeInteger().
{
const example = document.querySelector("#guide").cloneNode();
function readCount(element) {
const raw = element.getAttribute("data-count");
if (raw === null || !/^[0-9]+$/.test(raw.trim())) return null;
const count = Number(raw);
return Number.isSafeInteger(count) ? count : null;
}
console.log(readCount(example)); // 3
example.setAttribute("data-count", "0");
console.log(readCount(example)); // 0
example.setAttribute("data-count", " ");
console.log(readCount(example)); // null
example.removeAttribute("data-count");
console.log(readCount(example)); // null
}
The outputs are 3, 0, null and null. The guard prevents a whitespace-only string from becoming zero. Negative, fractional, hexadecimal and out-of-range values also return null. Check count === null to keep a genuine zero.




