jQuery :focus Selector

The jQuery :focus selector selects all the elements that are currently focused on. There are many elements inside the form. However, when you have to select only the elements which have focus, you have to use this selector as given in the example below.

jQuery :focus Selector

Syntax of the Selector

The syntax of jQuery :focus selector is given below.

Syntax
$(“:focus”)

The selector requires no arguments to pass with it to use. However, if you want to select only the specified element, you have to specify the tag name, class, or id of the element. If the form contains a single element, you don’t have to specify the tag name with the selector.

jQuery :focus Selector Selects All Focused Elements

If you want to select focused elements in a form, you have to use this selector. If the form contains more than one input, it focuses the last input element. See the example given below to find the focused element using this selector.

Example

Test it Live

Output




The above example showing the single element as the focused element. It’s the last input element of the form. jQuery applies the background color as ‘yellow’ to the element. You can apply other effects to the input elements using this selector.

I hope you like this tutorial post of jQuery :focus selector. If you have any queries regarding this tutorial post, please comment below.

Also tell me, what other methods you are using with the selector by commenting below.

References