jQuery Event - focus() Method

Example

Change the background color of the input box when it gains focus:

$("input").focus(function(){
  $("input").css("background-color","#FFFFCC");
});

Try It Yourself

Definition and Usage

A focus event occurs when an element gains focus.

When an element is selected by mouse click or located by the tab key, it will gain focus.

The focus() method triggers the focus event or specifies the function to be executed when the focus event occurs.

Trigger the focus event

Syntax

$(selector).focus()

Try It Yourself

Bind the function to the focus event

Syntax

$(selector).focus(function)
Parameter Description
function Optional. Specifies the function to be executed when the focus event occurs.

Try It Yourself