Categories
JavaScript Answers

How to focus input box on load with JavaScript?

Spread the love

Sometimes, we want to focus input box on load with JavaScript.

In this article, we’ll look at how to focus input box on load with JavaScript.

How to focus input box on load with JavaScript?

To focus input box on load with JavaScript, we can call the input element’s focus method.

For instance, we write

<input id="myinputbox" type="text" />

to add an input element.

Then we write

window.onload = () => {
  document.getElementById("myinputbox").focus();
};

to select the input with getElementById.

Then we call focus on it to focus it.

We put the code in window.onload to run it when the input is loaded.

Conclusion

To focus input box on load with JavaScript, we can call the input element’s focus method.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.