JSFiddle - React, Tailwind, and code Playground

by Rafael Boschini

HTML

<div contentEditable>
    Teste exemplo ok yeah no
</div>

<input type="button"value="ok">

JavaScript

$(document).ready(function(){
    
	$('input[type=button]').on('click',function(){
    	GetSelectedText();
    });
    
});

function GetSelectedText() {
            if (document.getSelection) {    // all browsers, except IE before version 9
                var sel = document.getSelection ();
                    // sel is a string in Firefox and Opera, 
                    // and a selectionRange object in Google Chrome, Safari and IE from version 9
                    // the alert method displays the result of the toString method of the passed object
                alert (sel);
            } 
            else {
                if (document.selection) {   // Internet Explorer before version 9
                    var textRange = document.selection.createRange ();
                    alert (textRange.text);
                }
            }
        }