JavaScript Select the contents of an element


Example

let sel = document.getSelection();

let myNode = document.getElementById('element-to-select');

let range = document.createRange();
range.selectNodeContents(myNode);

sel.addRange(range);

It may be necessary to first remove all the ranges of the previous selection, as most browsers don't support multiple ranges.