focus() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:focus

Description

The focus() method triggers the focus event, or sets function as focus event handler.

Syntax

$(selector).focus(function);
Parameter Require Description
function Optional. function to call when the focus event occurs

The following code shows how to attach a function to the focus event. The focus event occurs when the <input> field gets focus:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("input").focus(function(){
        $("span").css("display", "inline").fadeOut(2000);
    });/*from w ww . java2s.c o  m*/
});
</script>
<style>
span {
    display: none;
}
</style>
</head>
<body>

<input type='text'>

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

</body>
</html>

Related Tutorials