diff --git a/snippets/getSelectedText.md b/snippets/getSelectedText.md new file mode 100644 index 000000000..fdd6676b3 --- /dev/null +++ b/snippets/getSelectedText.md @@ -0,0 +1,16 @@ +--- +title: getSelectedText +tags: browser,beginner +--- + +Get the currently selected text. + +Use `window.getSelection()` and `Selection.prototype.toString()` to get the currently selected text. + +```js +const getSelectedText = () => window.getSelection().toString(); +``` + +```js +getSelectedText(); // 'Lorem ipsum' +```