close

How to set focus on an input field after rendering?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to set focus on an input field after rendering? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to set focus on an input field after rendering ?

  1. How to set focus on an input field after rendering ?

    to set focus on an input field after rendering This is not longer the best answer. As of v0.13, this.refs may not available until AFTER componentDidMount() runs, in some odd cases.

  2. to set focus on an input field after rendering

    to set focus on an input field after rendering This is not longer the best answer. As of v0.13, this.refs may not available until AFTER componentDidMount() runs, in some odd cases.

Method 1

answer is correct, and for convenience you can use the autoFocus prop to have an input automatically focus when mounted:

<input autoFocus name=...

Note that in jsx it’s autoFocus (capital F) unlike plain old html which is case-insensitive.

Method 2

This is not longer the best answer. As of v0.13, this.refs may not available until AFTER componentDidMount() runs, in some odd cases.

Just add the autoFocus tag to your input field, as FakeRainBrigand showed above.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment