BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

jQuery focus() method

Last Updated: May 1, 2019 by Chaitanya Singh | Filed Under: jQuery

jQuery focus() method attaches an event handler function to html elements. This event handler function executes when a form field gets focus.

jQuery focus() method Syntax

$("input").focus(function(){
  //code to execute when the focus event is triggered
});

jQuery focus() Example

In the following example we have two input form fields, we want to change the background colour of the form field when the field gets focus (when we click inside the box), to do this, we have attached the event handler function where we are changing the background colour of html elements, to the input html element. This way when any of the form field gets focus, its background colour gets changed to colour value #444.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("input").focus(function(){
    $(this).css("background-color", "#444");
  });
});
</script>
</head>
<body>

<h2>jQuery focus event example on Beginnersbook.com</h2>
Your Name: <input type="text" name="name"><br>
Your Age: <input type="text" name="age">
</body>
</html>

Output:
Before any form field gets focus:
This screenshot is taken when the html page is initially loaded and we have not clicked on any form field:
jQuery focus method
After the first form field gets focus:
This screenshot is taken after we have clicked inside first form field, the form field gets focus so its background colour is changed to #444 as set in the event handler function.
jQuery focus event

❮ PreviousNext ❯

Top Related Articles:

  1. jQuery html() method
  2. jQuery Sliding Effects
  3. jQuery keydown() Method
  4. jQuery hover() method
  5. jQuery keypress() Method

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

jQuery Tutorial

  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events

jQuery Effects

  • jQuery Show & Hide
  • jQuery Fading
  • jQuery Sliding
  • jQuery Animate
  • jQuery stop()
  • jQuery Callback Function
  • jQuery Chaining

jQuery HTML/CSS

  • jQuery html()
  • jQuery addClass()
  • jQuery hasClass()
  • jQuery removeClass()
  • jQuery toggleClass()
  • jQuery after()
  • jQuery before()
  • jQuery append()
  • jQuery appendTo()
  • jQuery clone()
  • jQuery insertBefore()
  • jQuery insertAfter()
  • jQuery attr()
  • jQuery text()
  • jQuery val()
  • jQuery css()
  • jQuery prepend()
  • jQuery remove()
  • jQuery empty()
  • jQuery detach()

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap