JSFiddle - React, Tailwind, and code Playground

by Shannenpio Cloning

HTML

<div id="foo">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>
<button id="btn">Select</button>

CSS

body {
    padding:50px;
    background-color:white;
}
div {
    border:1px solid;
    padding:4px;
    margin:0 0 1em;
}

JavaScript

function autoSelect(elem) {
    var selection, range;
    if (window.getSelection) {
        selection = window.getSelection();
        range = document.createRange();
        range.selectNodeContents(elem);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if (document.selection) { // IE
        selection = document.selection.createRange().text;
        range = document.body.createTextRange();
        range.moveToElementText(elem);
        range.select();
    }
}

// Eksekusi di sini
document.getElementById('btn').onclick = function() {
    autoSelect(document.getElementById('foo'));
};