Add getSelectedText
This commit is contained in:
16
snippets/getSelectedText.md
Normal file
16
snippets/getSelectedText.md
Normal file
@ -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'
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user