CaretPosition: offsetNode property

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("CSSOM view API")}}

The **offsetNode** property of the {{domxref("CaretPosition")}} interface returns a {{domxref("Node")}} containing the found node at the caret's position.

Value

A {{domxref("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" />
html hidden
css hidden
js
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}`);
});
js hidden

{{EmbedLiveSample("Examples", "", 300)}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{domxref("Node")}}
  • {{domxref("Document.caretPositionFromPoint()")}}