The offsetNode property of the CaretPosition interface returns a Node containing the found node at the caret's position.
A Node.
This example logs the offsetNode and offset of the caret position when clicking inside the input field.
<input aria-label="text field" value="Click inside this input field" />document.querySelector("input").addEventListener("click", (event) => {
const x = event.clientX;
const y = event.clientY;
const caret = document.caretPositionFromPoint?.(x, y);
if (!caret) {
log("Not supported");
return;
}
const node = caret.offsetNode;
const offset = caret.offset;
log(`offsetNode: ${node}`);
log(`offset: ${offset}`);
});