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.