JSFiddle - React, Tailwind, and code Playground
HTML
<div>
<p>Select something in here with the mouse. The selection will clear after a second.</p>
<p>Try it an a text input: <input value="Some text"></p>
<p>Try it an a text input: <textarea rows="5" cols="30">Some text</textarea></p>
</div>
JavaScript
function clearSelection() {
var sel;
if ( (sel = document.selection) && sel.empty ) {
sel.empty();
} else {
if (window.getSelection) {
window.getSelection().removeAllRanges();
}
var activeEl = document.activeElement;
if (activeEl) {
var tagName = activeEl.nodeName.toLowerCase();
if ( tagName == "textarea" || (tagName == "input" && activeEl.type == "text") ) {
// Collapse the selection to the end
activeEl.selectionStart = activeEl.selectionEnd;
}
}
}
}
document.onmouseup = function() {
window.setTimeout(clearSelection, 1000);
};