M
M
maratt2020-01-28 15:10:52
React
maratt, 2020-01-28 15:10:52

How to highlight text in input?

When you click on the input, you need to select the text inside, I do this

const onFocus = (e) => e.target.select();
<input onFocus={onFocus}>

But the problem is that at the first focus the text is selected. But if you click anywhere, except for the input, and then again on the input, then it no longer selects.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Cheryabushkin, 2020-01-28
@Floatname

<input type="text" onFocus="this.select()">

M
Modin, 2020-01-28
@Modin

const input = React.useRef();
<input
        type="text"
        ref={input}
        onFocus={() => input.current.select()}
  />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question