Categories
JavaScript Answers

How to set focus on an element in an HTML form using JavaScript?

Spread the love

To set focus on an element in an HTML form using JavaScript, we call the input’s focus method.

For instance, we write

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

to add an input.

Then we write

document.getElementById("myText").focus();

to select the input with getElementById.

Then we call focus to set focus on it.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.