jQuery set focus | How to set focus to html element using jQuery

jQuery set focus method

The focus() method can be used to set the focus to an element of HTML. This usually requires while filling a form and then notifying a user about the certain action to take in order to correct input field.

Example of using set focus method

In the following example, the focus is set in the document.ready() event of jQuery. The demo page contains two forms fields that are textboxes. By using the focus method, the focus is set to the second textbox as web page loads:

Have a look by clicking the link or image below:

jQuery set focus

Experience this example online



Experience this example online

You see, we simply used the



inside the document.ready() where txtfocus is the ID of the second textbox. Similarly, you may set focus to any HTML element upon certain user action like clicking a button, link or pressing the submit button etc.

Useful reading: jQuery focus method()


Was this article helpful?

Related Articles

Leave A Comment?