CKTIS / JavaScript
Selection: count and add ranges with rangeCount and addRange()
`rangeCount` tells you how many ranges a document selection contains, and `addRange()` adds a DOM range. To replace the selection with one range across browsers, clear it first.
Version: Current web platform; runtime support varies
Syntax
// Web platform: Selection API (not part of ECMAScript).
const selection = window.getSelection(); // Selection | null
if (selection !== null) {
const count = selection.rangeCount; // read-only number
selection.addRange(range); // takes a Range; returns undefined
}Selection.rangeCount/addRange pairs a read-only property with a method on the document’s Selection object. The object represents the document selection or caret position. Get it with window.getSelection() and check for null. This is a web-platform API, not part of ECMAScript 2024. For text inside a textarea or a supported input type, use the element’s setSelectionRange() instead.
rangeCount is a number that counts Range objects, not characters. 0 means there is no range. You can still have rangeCount === 1 with no highlighted text: a range collapsed to a single point still counts. Check that rangeCount > 0 before calling getRangeAt(0), because an out-of-bounds index throws.
addRange(range) takes one Range object and returns undefined, so its return value does not tell you whether the range was added. Create a range with document.createRange() and set its boundaries, for example with range.selectNodeContents(node) on a node attached to the same document. A Range is live: DOM edits can adjust its boundaries.
The specification allows at most one range per selection, but Firefox supports multiple ranges. A browser limited to one range can ignore another addRange() call rather than replace the existing range. To set one new range reliably, call removeAllRanges() first. This clears the previous selection, not the document content.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Selection API example</title>
</head>
<body>
<script>
(() => {
const selection = window.getSelection();
if (selection === null) throw new Error("No document selection available");
const paragraph = document.createElement("p");
paragraph.textContent = "CKTIS";
document.body.append(paragraph);
try {
const range = document.createRange();
range.selectNodeContents(paragraph);
selection.removeAllRanges();
console.log(selection.rangeCount);
selection.addRange(range);
console.log(selection.rangeCount);
console.log(selection.toString());
} finally {
selection.removeAllRanges();
paragraph.remove();
}
})();
</script>
</body>
</html>Result
0 1 CKTIS
Save the example as an `.html` file, open it in a separate browser tab, and check the developer console. It logs `0` after clearing the selection, then `1` and `CKTIS` after adding the range. It clears the selection and removes the temporary paragraph at the end; it does not restore any previous selection.
Related lessons
- JavaScript · Lesson 33 · Intermediate · Version: 3