Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Text & highlight selection or get selection value (React)

I have a React application which displays some spans:

<span>Hello</span> <span>my</span> <span>name</span> <span> is </span> ...

I would like the user to select the text with the mouse like so Hello my Name is

..and then get the selected value, or highlight the text etc. How would I do this in React? I am not sure what event handlers to use and how to get hold of the current selection! A minimal example or a hint would be appreciated!

like image 210
George Welder Avatar asked Apr 03 '17 12:04

George Welder


1 Answers

I think that it's the right way...

 document.onmouseup = () => {
   console.log(window.getSelection().toString());
 };
like image 156
JuliSmz Avatar answered Sep 21 '22 11:09

JuliSmz