Posts Learn Components Snippets Categories Tags Tools About
/

How to Get the User Selection Text on a Web Page

Learn how to get the user selection text on a web page the easy way

Created on Jun 28, 2022

31 views

If you want to get the text that a user selects or highlights on a web page, there is a useful one-liner for that and it's making use of the "window.getSelection" way.
const getSelectedText = () => window.getSelection().toString();

console.log(getSelectedText());

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components
)