Sometimes, we want to get selected text in JavaScript.
In this article, we’ll look at how to get selected text in JavaScript.
How to get selected text in JavaScript?
To get selected text in JavaScript, we can use the document.selection.createRange
or window.getSelection
methods.
For instance, we write
document.selection.createRange().text
or
window.getSelection()
to return the currently selected text.
Conclusion
To get selected text in JavaScript, we can use the document.selection.createRange
or window.getSelection
methods.