Home >>jQuery Tutorial >jQuery focus() Method

jQuery focus() Method

jQuery focus() Method

jQuery focus() method in jQuery is used to triggers or add a function to run when a focus event occurs.

Syntax:
Trigger the focus event for selected elements: $(selector).focus()
Attach a function to the focus event: $(selector).focus(function)

Parameter Values

Parameter Description
function It is an optional parameter and is used to specifies the function to run when the focus event occurs
Here is an Example of jQuery focus() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").focus(function(){
$(".txt").css("display", "inline").fadeOut(2000);
});
});
</script>
<style>
span {
display: none;
}
</style>
</head>
<body>
<input>
<span class="txt">how are you!</span>
<p class="txt1">Click in the above input field to get focus.</p>
</body>
</html>

Output:
how are you!

Click in the above input field to get focus.


No Sidebar ads