Categories
JavaScript Answers

How to get selected text in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *