JSFiddle - React, Tailwind, and code Playground

by lau_chung

HTML

<div>Text in div</div>
<textarea>Hello textarea</textarea>
<div id='debug'></div>

CSS

#debug{
    background-color:#EEE;
}

JavaScript

$(document).on('mouseup','body',function(){
   $("#debug").html("You select '" + getSelectionText() + "'");
});

function getSelectionText() {
    if (window.getSelection) {
        try {
            var ta = $('textarea').get(0);
            return ta.value.substring(ta.selectionStart, ta.selectionEnd);
        } catch (e) {
            console.log('Cant get selection text')
        }
    } 
    // For IE
    if (document.selection && document.selection.type != "Control") {
        return document.selection.createRange().text;
    }
}