'How can I select all text in a div after focusing using a ref in react?

In a nutshell, I'm trying to select all text after setting it to contentEditable = true and focusing, all using a ref. Ideally, I would do this using an onFocus event. I was unable to find any solutions online. Thanks in advance!



Solution 1:[1]

Did you try using select() like this-

<refField>.current.select()

You may want to add above line in useEffect and contentEditable as its dependency.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dev_sanketsr