• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery focusin – Changing the Background Color with the focusin() Method

jQuery focusin – Changing the Background Color with the focusin() Method

February 10, 2022 Leave a Comment

The jQuery focusin method will occur when an element (most of the time an input) gets the user’s focus. This occurs when either the user mouse clicks onto the element, or tabs onto it using the keyboard.

$("input").focusin(function(){
  //do something
});

Let’s say we have the following HTML:

<form>
  <label for="fname">Full Name:</label>
  <input type="text" id="fname" name="fname">
  <button type="submit" value="Submit">Submit</button>
</form>

To highlight the input field when the user clicks on it, we can use the jQuery focusin() method.

Once the user clicks on the input field, the focusin() method will be triggered, and we can change its background color.

$("#fname").focusin(function(){
  $("#fname").css('background','green');
});

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#fname").focusin(function(){
  jQuery("#fname").css('background','green');
});

This can also be done using the jQuery focus() method.

An Interactive Example of the jQuery focusin() Method

Below we will set up a simple form with a name field and a submit button.

Here is the HTML code:

<form>
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname">
  <button type="submit" value="Submit">Submit</button>
</form>

When the user clicks or tabs into the name field, we will change the background color to light blue. When they click away, we will use the focusout() method to reset the background.

Here is the code JavaScript code:

$("#fname").focusin(function(){
  $("#fname").css('background','#d1e2f3');
});

$("#fname").focusout(function(){
  $("#fname").css('background','#fff');
});

The final code and output for this example of using the jQuery focusin() method is below:

Code Output:




Full Code:

<form>
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname">
  <button type="submit" value="Submit">Submit</button>
</form>

<script>

$("#fname").focusin(function(){
  $("#fname").css('background','#d1e2f3');
});

$("#fname").focusout(function(){
  $("#fname").css('background','#fff');
});

</script>

Hopefully this article has been useful to help you understand how to use the jQuery focusin() method.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Hide a Div
  • 2.  Using jQuery to Remove Attribute from HTML Element
  • 3.  How to Use jQuery to Clear a textarea Field
  • 4.  Using jQuery to Swap an Image on Hover
  • 5.  jQuery hover – An Interactive Example of the hover Method
  • 6.  jQuery Opacity – How to Change the Opacity of an Element
  • 7.  How to Use jQuery to Change Button Text
  • 8.  jQuery parent – Get the Parent Element of a Div
  • 9.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery
  • 10.  Using jQuery to Toggle Class of HTML Element

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy