Get selected text
![](/assets/cover/white-tablet-2-800.webp)
Gets the currently selected text.
- Use
Window.getSelection()
andSelection.toString()
to get the currently selected text.
const getSelectedText = () => window.getSelection().toString(); getSelectedText(); // 'Lorem ipsum'
Gets the currently selected text.
Window.getSelection()
and Selection.toString()
to get the currently selected text.const getSelectedText = () => window.getSelection().toString(); getSelectedText(); // 'Lorem ipsum'
Here's a quick and easy way to select the currently focused DOM element in JavaScript.
Learn a simple way to get the browser's current URL in JavaScript.
Calculate the coordinates of the current scroll position in the browser window using JavaScript.
Learn how to programmatically copy text to clipboard with a few lines of JavaScript and level up your web development skills.