JSFiddle - React, Tailwind, and code Playground

by Kai

HTML

<p id="words">This is a sentence made up of several words. Several words form several sentences. Eventually, several sentences will form a paragraph. But there won't be enough for that here.</p>

CSS

#words {
    padding: 6px;
    background-color: #ffffe0;
    border: 1px solid #ccc;
    text-align: justify;
    border-radius: 5px;
}

JavaScript

$('#words').bind('mouseup', function () {
    var $this = $(this),
        selection;
    
    if (window.getSelection) {
        selection = window.getSelection();
    } else if (document.selection) {
        selection = document.selection.createRange();
    }
    
    
    alert(selection.toString());
});