JavaScript Selection API Get the text of the selection

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 Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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.log(document.getSelection());


Got any JavaScript Question?