How to select all text in input with Reactjs, when it focused?

JavascriptSelectInputReactjs

Javascript Problem Overview


For example: codepen

var InputBox = React.createClass({
  render: function() {
    return (
      <input className="mainInput" value='Some something'></input>
    )
  }
});

Javascript Solutions


Solution 1 - Javascript

Functional component:

const handleFocus = (event) => event.target.select();
const Input = (props) => <input type="text" value="Some something" onFocus={handleFocus} />

ES6 class component:

class Input extends React.Component {
    handleFocus = (event) => event.target.select();

    render() {
        return (
            <input type="text" value="Some something" onFocus={this.handleFocus} />
        );
    }
}

React.createClass:

React.createClass({
    handleFocus: function(event) {
      event.target.select();
    },

    render: function() {
      return (
        <input type="text" value="Some something" onFocus={this.handleFocus} />
      );
    },
})

Solution 2 - Javascript

Another Way Functional Component with useRefHook:

const inputEl = useRef(null);

function handleFocus() {
  inputEl.current.select();
}

<input
  type="number"
  value={quantity}
  ref={inputEl}
  onChange={e => setQuantityHandler(e.target.value)}
  onFocus={handleFocus}
/>

Solution 3 - Javascript

Thanks, I appreciate it. I did it so:

var input = self.refs.value.getDOMNode();
            input.focus();
            input.setSelectionRange(0, input.value.length);

Solution 4 - Javascript

var InputBox = React.createClass({
  getInitialState(){
    return {
      text: ''
    };
  },
  render: function () {
    return (
      <input
        ref="input"
        className="mainInput"
        placeholder='Text'
        value={this.state.text}
        onChange={(e)=>{this.setState({text:e.target.value});}}
        onFocus={()=>{this.refs.input.select()}}
      />
    )
  }
});

You have to set ref on the input and when focused you have to use select().

Solution 5 - Javascript

var React = require('react');

var Select = React.createClass({
    handleFocus: function(event) {
        event.target.select()
    },
    render: function() {
        <input type="text" onFocus={this.handleFocus} value={'all of this stuff'} />
    }
});

module.exports = Select;

Auto select all content in a input for a react class. The onFocus attribute on a input tag will call a function. OnFocus function has a parameter called event generated automatically. Like show above event.target.select() will set all the content of a input tag.

Solution 6 - Javascript

In my case I wanted to select the text from the beginning after the input appeared in the modal:

componentDidMount: function() {
    this.refs.copy.select();
},

<input ref='copy'

Solution 7 - Javascript

let's add the simplest based on @dschu's answer:

...
<input type='text' value='Some something' onFocus={e => e.target.select()} />
...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexander ShtangView Question on Stackoverflow
Solution 1 - JavascriptdschuView Answer on Stackoverflow
Solution 2 - JavascriptIgor PavlenkoView Answer on Stackoverflow
Solution 3 - JavascriptAlexander ShtangView Answer on Stackoverflow
Solution 4 - Javascriptobreja catalinView Answer on Stackoverflow
Solution 5 - JavascriptKylo JorgensenView Answer on Stackoverflow
Solution 6 - JavascriptLukas LukacView Answer on Stackoverflow
Solution 7 - JavascriptFacundo ColombierView Answer on Stackoverflow