JavaScript Selection API Select the contents of an element

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any JavaScript Question?