CaretPosition: offsetNode property
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
The offsetNode
property of the CaretPosition
interface returns a Node
containing the found node at the caret's position.
Value
A Node
.
Examples
This example logs the offsetNode
and offset
of the caret position when clicking inside the input field
html
<input
aria-label="text field"
value="Click inside this input field"
style="width: 100%; padding: 10px; font-size: 16px; box-sizing: border-box" />
js
document.querySelector("input").addEventListener("click", (event) => {
const x = event.clientX;
const y = event.clientY;
const caret = document.caretPositionFromPoint?.(x, y);
if (!caret) return;
const node = caret.offsetNode;
const offset = caret.offset;
console.log("offsetNode:", node);
console.log("offset:", offset);
});
Specifications
Specification |
---|
CSSOM View Module # dom-caretposition-offsetnode |