JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://rangy.googlecode.com/svn/trunk/currentrelease/rangy-core.js"></script>
<div>Nothing can be selected in here</div>
<div id="selectable">Stuff can be selected in here</div>
<div>Nothing can be selected in here</div>
JavaScript
function limitSelectionToElement(el) {
var selectedRange = null;
var sel = rangy.getSelection();
var elRange = rangy.createRange();
elRange.selectNodeContents(el);
if (sel.rangeCount) {
selectedRange = sel.getRangeAt(0).intersection(elRange);
}
elRange.detach();
sel.setSingleRange(selectedRange);
}
document.onmouseup = function() {
limitSelectionToElement(document.getElementById("selectable"));
};