Phong Duong

🔎Search

Get the currently selected text with Javascript

11-03-2021 | #javascript

I am looking for how to get the currently selected text. I find select event. But it only fires after text inside an <input type="text"> or <textarea> is selected.

Finally, I figure out the solution for this issue. You can call getSelection() method of window after user clicks mouse.

getSelection() method returns a Selection object. To get the selected text, you call its toString() method.

Share: TwitterFacebookLinkedInHacker News