Get selected text

Angelos Chalaris · JavaScript, Browser · Oct 19, 2020

Gets the currently selected text.

  • Use Window.getSelection() and Selection.toString() to get the currently selected text.
const getSelectedText = () => window.getSelection().toString();
getSelectedText(); // 'Lorem ipsum'

Recommended snippets