CKTIS / JavaScript
Element scrolling and child replacement: scrollTop, scrollHeight, and replaceChildren()
Read and set an element’s scroll position, distinguish the full scrollable height from the visible inner height, and replace child nodes without parsing strings as HTML.
Version: Current web platform; runtime support varies
Syntax
const offset = el.scrollTop;
el.scrollTop = y;
const contentHeight = el.scrollHeight;
const visibleHeight = el.clientHeight;
el.replaceChildren();
el.replaceChildren(node, "text");
el.replaceChildren(...items);Element.scrollTop/scrollHeight/replaceChildren groups two properties and a method. scrollTop is a read/write vertical offset from the scroll origin, in CSS pixels, and can be fractional. Assignment requests an absolute position, not a relative move, with scroll behavior auto. Values can be negative with flex-direction: column-reverse, and Safari’s bounce can temporarily take them outside the usual range.
scrollHeight is a read-only integer height of the entire scrolling area, including the part out of view. It counts padding and can include pseudo-elements, but excludes the element’s border, margin, and horizontal scrollbar. clientHeight measures the visible inner height. With no vertical overflow, the two heights are equal.
In an ordinary downward-scrolling container, el.scrollHeight - el.clientHeight approximates the bottom scrollTop. Check for the bottom with Math.abs(el.scrollHeight - el.clientHeight - el.scrollTop) <= 1 rather than ===: the heights are rounded, but the offset can be fractional. Do not use that test unchanged for a reversed layout or during overscroll bounce.
replaceChildren() replaces all direct child nodes, not the element itself. It accepts any number of Node objects or strings, moves existing nodes rather than cloning them, and turns strings into Text nodes without parsing HTML. With no arguments it empties the element. It returns undefined; an insertion that violates tree hierarchy can throw HierarchyRequestError (a DOMException).
Replace the content first, then measure the new layout and set the scroll position, because the scroll range may change. ECMAScript 2024 is the JavaScript language baseline, not a version of these web-platform APIs. The DOM Standard defines replaceChildren(), and CSSOM View defines scrollTop, scrollHeight, and clientHeight. Support depends on the browser version, and the measurements require a layout engine.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Element scrolling and child replacement</title>
</head>
<body>
<script>
(() => {
const box = document.createElement('div');
box.style.cssText = 'width:200px;height:100px;padding:0;border:0;overflow-x:hidden;overflow-y:auto;scroll-behavior:auto';
box.textContent = 'Old content';
document.body.append(box);
try {
const row = document.createElement('div');
row.style.height = '160px';
box.replaceChildren(row);
const end = box.scrollHeight - box.clientHeight;
box.scrollTop = end;
const atBottom = Math.abs(end - box.scrollTop) <= 1;
console.log(`${box.scrollHeight} ${box.clientHeight} ${atBottom}`);
box.replaceChildren('<b>Done</b>');
console.log(`${box.textContent} | ${box.children.length}`);
box.replaceChildren();
console.log(box.childNodes.length);
} finally {
box.remove();
}
})();
</script>
</body>
</html>Result
160 100 true <b>Done</b> | 0 0
Save the example as an HTML file, open it in a browser, and check the developer console. The heights 160 and 100 give a scroll range of 60 CSS pixels. `true` means the position is within the bottom tolerance. The markup is logged as literal text, and the final `0` shows that no child nodes remain.
Related lessons
- JavaScript · Lesson 42 · Advanced · Version: 4