How to focus on input HTML tag using JavaScript?

October 22, 2020 - 1 min read

Focusing on the input tag is a common feature in modern websites to give attention to the user visiting.

To set focus on an input tag element in HTML, you can use the focus() method on the element using JavaScript.

Let's say we have an input tag in HTML like this,

<!-- Input tag -->
<input id="myInput" type="text" placeholder="Enter some text here" />

And we want the input tag to be on focus whenever the user visits the website.

Let's focus the input tag element using focus() method like this,

// first get a reference to the input tag
const input = document.querySelector("#myInput");

// use the focus method
input.focus();

Now the input tag is focussed. ⭐️

See this example live in JSBin.

Feel free to share if you found this useful 😃.