jQuery focus()

Introduction

Attach a function to the focus event.

The focus event occurs when the <input> field gets focus:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//from   w w  w  . j ava  2s. c om
<script>
$(document).ready(function(){
  $("input").focus(function(){
    $("span").css("display", "inline").fadeOut(2000);
  });
});
</script>
<style>
span {
  display: none;
}
</style>
</head>
<body>

<input>

<span>Hi!</span>
<p>Click in the input field to get focus.</p>

</body>
</html>

The focus event occurs when an element gets focus.

The focus() method triggers the focus event.

The focus() method also run a function when a focus event occurs.

Trigger the focus event for selected elements:

$(selector).focus()

Attach a function to the focus event:

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



PreviousNext

Related