document.caretPositionFromPoint(x, y) maps a point in the viewport to a text insertion position. It returns a CaretPosition with the node (offsetNode) and character offset (offset), or null.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.caretPositionFromPoint. Start with this line.
JavaScript
document.caretPositionFromPoint(x, y[, options])
Call
What it does
document.caretPositionFromPoint(x, y[, options])
Primary signature.
document.caretPositionFromPoint(...)
Call with the arguments your case needs.
Built into the browser. Call caretPositionFromPoint(...) on document — no import required.
Library declarations
JavaScript
document.caretPositionFromPoint(x, y[, options])
Parameters
Parameter
Description
x, y
Required. Viewport coordinates (CSS pixels)
options.shadowRoots
Optional. Shadow roots that may contain the caret
Return Value
Return Value
Description
*
CaretPosition or null
JavaScript Examples
Example 1 — Read offsetNode and offset
On click, map the pointer to a caret position inside the text.
JavaScript
document.querySelector("p").addEventListener("click", (e) => {
if (!document.caretPositionFromPoint) return;
const pos = document.caretPositionFromPoint(e.clientX, e.clientY);
if (!pos) {
console.log("null");
return;
}
console.log(pos.offsetNode.nodeName); // "#text" when on text
console.log(pos.offset); // character index
});
Feature-detect. Newly available APIs need a typeof check on older browsers.
Invalid coordinates. Negative or out-of-viewport points return null (MDN).
No insertion point. Some UI areas also return null.
Always null-check before reading offsetNode.
Compatibility
Browser Support
Document.caretPositionFromPoint() is a browser API marked Baseline 2025 Newly available on MDN (since December 2025). Feature-detect; keep caretRangeFromPoint as a legacy fallback where needed.
✓ Baseline 2025 · Newly available
Document.caretPositionFromPoint()
Supported in the latest major browsers. Older engines may only expose the non-standard caretRangeFromPoint fallback.
It returns a CaretPosition object (or null) for the text insertion point at viewport coordinates x and y — including the DOM node and character offset within that node.
No. MDN marks Document.caretPositionFromPoint() as Baseline 2025 Newly available (since December 2025). It is not Deprecated, Experimental, or Non-standard. Older browsers may still lack it — feature-detect.
A CaretPosition with offsetNode and offset, or null if there is no viewport, coordinates are invalid/outside the viewport, or no text insertion point exists there.
caretPositionFromPoint() is the standard method returning CaretPosition. caretRangeFromPoint() is a non-standard WebKit-era fallback that returns a Range (startContainer / startOffset).
An optional array of ShadowRoot objects. When supplied, the method can return a caret position inside those shadow trees. Otherwise positions inside other shadow DOM may remap to the shadow host.
Use viewport coordinates — typically event.clientX and event.clientY from a click or pointer event, not pageX/pageY.