Home >>jQuery Tutorial >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 | Description |
---|---|
function | It is an optional parameter and is used to specifies the function to run when the focus event occurs |
<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>
Click in the above input field to get focus.