Example

Change background color of an input field when it gets focus:

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

Try it yourself »

Definition and Usage

The focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it).

The focus() method triggers the focus event, or specifies a function to run when a focus event occurs.


Trigger the focus Event

Trigger the focus event for the selected elements.

Syntax

$(selector).focus()
Try it yourself »


Bind a Function to the focus Event

Specifies a function to run when the focus event occurs for the selected elements.

Syntax

$(selector).focus(function)
Try it yourself »

Parameter Description
function Optional. Specifies the function to run when the focus event occurs