9951 explained code solutions for 126 technologies


jqueryHow do I use jQuery to set focus on an element?


To set focus on an element using jQuery, you can use the .focus() method. This method triggers the focus event, or attaches a function to run when a focus event occurs.

Example code

$("#input-field").focus();

The code above sets focus on the element with an ID of input-field.

The code consists of three parts:

  1. $("#input-field") - This part selects the element with the ID of input-field.
  2. .focus() - This part triggers the focus event on the selected element.
  3. ; - This part ends the statement.

Helpful links

Edit this code on GitHub