select selection area

programmatic select

by popopome

HTML

<div>TEST programatic selection</div>
<div id="myDiv" style="color:red">The color of this text is red.</div>
<div>END of section</div>
<div>
    <button id="selectall">SELECT ALL</button>
</div>

JavaScript

function selectAll() {
    var div = $("#myDiv")[0];
    
    var range = document.createRange();
    range.selectNodeContents(div);
    
    var selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
}

$(function() {
    $('#selectall').on("click.selectall", 
                        function() {
                            selectAll();
                        });
});