JSFiddle - React, Tailwind, and code Playground
HTML
Press the button to select the pre-formatted text below:
<input type="button" onclick="selectElementContents(document.getElementById('test'))" unselectable="on" value="Select">
<pre id="test">
Here's some preformatted text
</pre>
More text
JavaScript
function selectElementContents(el) {
if (window.getSelection && document.createRange) {
// IE 9 and non-IE
var range = document.createRange();
range.selectNodeContents(el);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (document.body.createTextRange) {
// IE < 9
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.select();
}
}