CaretPosition: getClientRect() method

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

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

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

The getClientRect() method of the {{domxref("CaretPosition")}} interface returns the client rectangle for the caret range.

Syntax

js-nolint
getClientRect()

Parameters

None.

Return value

A {{domxref("DOMRect")}} object.

Examples

Get the caret's screen position

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 rect = caret.getClientRect();

  log(`Caret bounding rect: ${JSON.stringify(rect)}`);
  log(`Caret is at (${rect.x.toFixed(2)}, ${rect.y.toFixed(2)})`);
});
js hidden

{{EmbedLiveSample("Get the caret's screen position", "", 300)}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{domxref("DOMRect")}}
  • {{domxref("Document.caretPositionFromPoint()")}}
  • {{domxref("Element.getBoundingClientRect()")}}