CKTIS / JavaScript
Element.hasAttribute() and Element.scrollIntoView()
Check whether an element has a named attribute and use the result to decide whether to scroll to it. Distinguish attribute presence from its value, and scrolling from setting focus.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
element.hasAttribute(name)
element.scrollIntoView()
element.scrollIntoView(alignToTop)
element.scrollIntoView(options)Element.hasAttribute/scrollIntoView pairs a presence test with a scrolling action. element.hasAttribute(name) returns true if the named attribute is present at the time of the call, and false otherwise. Both an empty string and the string "false" count as present. Use getAttribute(name) to read the value (or get null if absent), and hasAttributeNS(namespace, localName) to test by namespace and local name. An HTML boolean attribute such as disabled means true when present; "false" is not a valid value for it and does not turn it off.
element.scrollIntoView() scrolls ancestor scroll containers, including the viewport as needed, to bring the element into view. It does not set focus or make a boxless element, such as one with display: none, render. Calling with no argument or with true corresponds to { block: "start", inline: "nearest" }; false changes block to "end".
In the options object, block and inline align along the logical block and inline axes. They accept "start", "center", "end", or "nearest"; the last limits the movement needed. behavior chooses animation ("smooth"), a jump ("instant"), or the CSS scroll-behavior setting ("auto", the default). The default container: "all" allows all scrollable ancestors to move; container: "nearest" limits scrolling to the closest one where supported. This is different from "nearest" alignment.
Both methods are called on an Element instance. They belong to the web platform, not ECMAScript 2024; check that an element lookup did not return null before calling. Option and return-value support varies by browser: scrollIntoView() can return a Promise that fulfills with an object containing the boolean interrupted. In implementations that return undefined, await alone does not wait for scrolling to end.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Conditional scrolling</title>
</head>
<body>
<script>
(() => {
const panel = document.createElement("div");
panel.style.cssText = "position:fixed;top:12px;left:12px;width:220px;height:96px;overflow:auto;border:1px solid;padding:0;writing-mode:horizontal-tb";
const spacer = document.createElement("div");
spacer.style.height = "240px";
const target = document.createElement("div");
target.style.height = "32px";
target.textContent = "Target";
target.setAttribute("data-jump", "false");
panel.append(spacer, target);
document.body.append(panel);
const before = panel.scrollTop;
for (const element of [spacer, target]) {
const marked = element.hasAttribute("data-jump");
console.log(String(marked));
if (marked) {
element.scrollIntoView({ behavior: "instant", block: "end", inline: "nearest" });
}
}
requestAnimationFrame(() => console.log(String(panel.scrollTop > before)));
})();
</script>
</body>
</html>Result
false true true
Save the example as an HTML file, open it in a browser, and check the developer console. `spacer` has no marker, while `target` has `data-jump` with the value `"false"`, so only `target` triggers scrolling. The third `true` reports that the panel's `scrollTop` increased; it is not the return value of `scrollIntoView()`.
Related lessons
- JavaScript · Lesson 37 · Advanced · Version: 4