JSFiddle - React, Tailwind, and code Playground
HTML
<div id="content-area">
How to Ask? <br />
Is your question about programming?<br />
We prefer questions that can be answered, not just discussed.<br />
Provide details. Share your research.<br />
If your question is about this website, ask it on meta instead.<br />
</div>
<br />
<div id="show-text"></div>
CSS
#content-area{padding:10px;border:1px dashed #f00;}
#show-text{color:#f00;}
JavaScript
function getSelected() {
if(window.getSelection) { return window.getSelection(); }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
}
$(document).ready(function() {
$('#content-area').mouseup(function() {
var selection = getSelected();
if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
$('#show-text').html(selection)
}
});
});