jQuery Focus() Event Method

jQuery focus event method; In this tutorial, You will learn how to use jQuery focus event with example.

jQuery Focus() Event Method

jQuery Focus event occurs when an element receives focus. This is generated by a mouse click or navigating it. And This event is applicable to a set of elements, such as form elements (<input>, <div>, <span>, <a>, <p>, <ul>, <li>, <button>, etc.).

Syntax jQuery Focus() Event Method

$(selector).focus()  

This triggers the focus event for the selected elements.

$(selector).focus(function)  

Parameters of jQuery Focus() Event Method

ParameterDescription
functionOptional. Specifies the function to run when the focus event occurs

Example of jQuery focus() event

Let’s see simple example with demonstrate jQuery focus() event.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Focus Example</title>
  <style>
  span {
    display: none;
  }
  </style>
   <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
 <p><input type="text"> <span>Focus starts.. Write your name.</span></p>
<p><input type="password"> <span>Focus starts.. Write your password.</span></p>
 <script>
$( "input" ).focus(function() {
  $( this ).next( "span" ).css( "display", "inline" ).fadeOut( 2000 );
});
</script>
 </body>
</html>

Recommended jQuery Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *