This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The Range.commonAncestorContainer read-only property returns the deepest — or furthest down the document tree — Node that contains both boundary points of the Range. This means that if Range.startContainer and Range.endContainer both refer to the same node, this node is the common ancestor container.
Since a Range need not be continuous, and may also partially select nodes, this is a convenient way to find a Node which encloses a Range.
This property is read-only. To change the ancestor container of a Node, consider using the various methods available to set the start and end positions of the Range, such as Range.setStart() and Range.setEnd().
A Node object.
In this example, we create an event listener to handle pointerup events on a list. The listener gets the common ancestors of each piece of selected text and triggers an animation to highlight them.
<ul>
<li>
Strings
<ul>
<li>Cello</li>
<li>
Violin
<ul>
<li>First Chair</li>
<li>Second Chair</li>
</ul>
</li>
</ul>
</li>
<li>
Woodwinds
<ul>
<li>Clarinet</li>
<li>Oboe</li>
</ul>
</li>
</ul>
The .highlight class created below uses a set of CSS @keyframes to animate a fading outline.
.highlight {
animation: highlight linear 1s;
}
@keyframes highlight {
from {
outline: 1px solid red;
}
to {
outline: 1px solid transparent;
}
}
document.addEventListener("pointerup", (e) => {
const selection = window.getSelection();
if (selection.type === "Range") {
for (let i = 0; i < selection.rangeCount; i++) {
const range = selection.getRangeAt(i);
playAnimation(range.commonAncestorContainer);
}
}
});
function playAnimation(el) {
if (el.nodeType === Node.TEXT_NODE) {
el = el.parentNode;
}
el.classList.remove("highlight");
setTimeout(() => {
el.classList.add("highlight");
}, 0);
}
| Specification |
|---|
| DOM> # ref-for-dom-range-commonancestorcontainer②> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
commonAncestorContainer |
1 | 12 | 1 | 9 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Range/commonAncestorContainer