JSFiddle - React, Tailwind, and code Playground
HTML
<div id="mt">
Keens' Women Venice H2 Sandals are a great pair of versatile and stylish sandals that include a range of exciting features. Topside they are constructed using an open strapping system, which provides outstanding ventilation. Underneath, a streamlined, grippy outsole features multi-directional lugs with razor siping, providing exceptional traction control on a variety of terrains.
The EVA metatomical molded footbed on the Venice H2 provides an outstanding level of comfort on all levels and are available in a range of stylish colours that will ensure you set the outdoors ablaze this summer. Once again, Keen has provided another fantastic product that you're guaranteed to love.
</div>
JavaScript
var getSelection = function() {
return $try(
function() { return window.getSelection(); },
function() { return document.getSelection(); },
function() {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
) || false;
};
var getCaretPosition = function(selection, el) {
var el = document.id(el) || window;
var text = el.get("text");
var offset = text.indexOf(selection);
return offset + selection.length;
}
document.id("mt").addEvent('mouseup',function(e) {
var selection = getSelection();
if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
console.log(selection);
var caretAt = getCaretPosition(selection, this);
console.log(caretAt);
}
});