Tutorial by Examples

let sel = document.getSelection(); sel.removeAllRanges();
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 s...
let sel = document.getSelection(); let text = sel.toString(); console.log(text); // logs what the user selected Alternatively, since the toString member function is called automatically by some functions when converting the object to a string, you don't always have to call it yourself. console...

Page 1 of 1