document.caretRangeFromPoint(x, y) maps a viewport point to a Range for the text insertion position. Read startContainer and startOffset — or get null when there is no valid caret there.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.caretRangeFromPoint. Start with this line.
JavaScript
document.caretRangeFromPoint(x, y)
Call
What it does
document.caretRangeFromPoint(x, y)
Primary signature.
document.caretRangeFromPoint(...)
Call with the arguments your case needs.
Built into the browser. Call caretRangeFromPoint(...) on document — no import required.
Library declarations
JavaScript
document.caretRangeFromPoint(x, y)
Parameters
Parameter
Description
x, y
Required. Viewport coordinates (CSS pixels)
Return Value
Return Value
Description
*
Range or null
JavaScript Examples
Example 1 — Read startContainer and startOffset
On click, map the pointer to a Range with the legacy method (when present).
JavaScript
document.querySelector("p").addEventListener("click", (e) => {
if (!document.caretRangeFromPoint) {
console.log("unsupported");
return;
}
const range = document.caretRangeFromPoint(e.clientX, e.clientY);
if (!range) {
console.log("null");
return;
}
console.log(range.startContainer.nodeName);
console.log(range.startOffset);
});
Feature-detect. Non-standard APIs are uneven across browsers.
Invalid coords. Negative or out-of-viewport points return null (MDN).
No text node. Some UI areas also return null.
Always guard before reading startContainer.
Compatibility
Browser Support
Document.caretRangeFromPoint() is Non-standard on MDN and not in any specification. Support is historically Chromium / WebKit oriented. Prefer the standard caretPositionFromPoint() API.
✓ Non-standard
Document.caretRangeFromPoint()
Legacy caret hit-test returning a Range. Feature-detect; do not assume Firefox or Safari parity with Chromium.
It returns a Range object (or null) for the document fragment under viewport coordinates x and y — typically with startContainer and startOffset describing the caret position.
MDN marks it Non-standard only — not Deprecated or Experimental. It is not part of any official specification. Prefer caretPositionFromPoint() when available.
Use document.caretPositionFromPoint() on supporting browsers. It is standard and can work with shadowRoots. Keep caretRangeFromPoint() only as a legacy fallback.
A Range, or null if x/y are negative, outside the viewport, or there is no text entry node at that point.
MDN warns it predates shadow DOM and can return unpredictable, implementation-specific results when ShadowRoot objects are present. Prefer caretPositionFromPoint() with shadowRoots.
For a caret at a single point: range.startContainer ≈ pos.offsetNode and range.startOffset ≈ pos.offset.