files/en-us/web/api/caretposition/getclientrect/index.md
{{APIRef("CSSOM view API")}}
The getClientRect() method of the {{domxref("CaretPosition")}} interface returns the client rectangle for the caret range.
getClientRect()
None.
A {{domxref("DOMRect")}} object.
<input aria-label="text field" value="Click inside this input field" />
<pre id="log"></pre>
input {
width: 100%;
padding: 10px;
font-size: 16px;
box-sizing: border-box;
}
#log {
height: 200px;
overflow: scroll;
padding: 0.5rem;
border: 1px solid black;
}
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)})`);
});
const logElement = document.querySelector("#log");
function log(text) {
logElement.innerText = `${logElement.innerText}${text}\n`;
logElement.scrollTop = logElement.scrollHeight;
}
{{EmbedLiveSample("Get the caret's screen position", "", 300)}}
{{Specifications}}
{{Compat}}