JSFiddle - React, Tailwind, and code Playground
HTML
<div onmouseup="alertSelectionTopLeft()">
<p>Hello, here is some text. Please select some of this text. Please select some of this text. Please select some of this text. Please select some of this text. Please select some of this text. Please select some of this text.</p>
<p> Please select some of this text. Please select some of this text. Please select some of this text. Please select some of this text. Please select some of this text.</p>
</div>
JavaScript
function getSelectionTopLeft() {
var x = 0, y = 0;
// Use standards-based method only if Range has getBoundingClientRect
if (window.getSelection && document.createRange && typeof document.createRange().getBoundingClientRect != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount > 0) {
var rect = sel.getRangeAt(0).getBoundingClientRect();
x = rect.left;
y = rect.top;
}
} else if (document.selection && document.selection.type != "Control") {
// All versions of IE
var textRange = document.selection.createRange();
x = textRange.boundingLeft;
y = textRange.boundingTop;
}
return { x: x, y: y };
}
function alertSelectionTopLeft() {
var topLeft = getSelectionTopLeft();
alert("(" + topLeft.x + ", " + topLeft.y + ")");
}