jQuery focus() Method - CodingTag
×


jQuery focus()

1343

  • The focus event of jQuery occurs when an HTML element gets focus. This event most often generated by the mouse click or we can also generate it by keyboard key pressing.
  • The focus() method is used to handle the focus event means this method is used to trigger the focus event or attaches a function to run when a focus event occurs.
  • This method is complimentary of the blur() jQuery method and most of the time both functions are used together.

Related Topics:

jQuery unbind
jQuery bind

jQuery focus()

Syntax:

$(selector).focus()

Or

If you want to trigger a function in it, the syntax will be:

$(selector).focus(function)

Parameter description:

  • function: It represents the function to run when the focus event occurs. It is optional.

Example:

<html>
<head>
<title> jQuery Example </title>
<script type = "text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$(".nwr").click(function(){
$(".name").blur();
$(".name").attr("placeholder","In blur mode.");
});
$(".wr").click(function(){
$(".name").focus();
$(".name").attr("placeholder","in focus mode");
});
});
</script>
</head>
<body>

<h2> jQuery Focus Event Example </h2>
Name: <input class="name" type="text" name="fullname">
<button class="wr"> Make focus </button>
<button class="nwr"> Make blur </button>
</body>
</html>

Output:

When clicking on the Make focus button,

When clicking on the Make blur button,



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments