• 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 focus – Changing Form Styles with the focus() Method

jQuery focus – Changing Form Styles with the focus() Method

February 10, 2022 Leave a Comment

The jQuery focus 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").focus(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 focus() method.

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

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

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

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

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

An Interactive Example of the jQuery focus() 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").focus(function(){
  $("#fname").css('background','#d1e2f3');
});

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

The final code and output for this example of using the jQuery focus() 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").focus(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 focus() method.

Other Articles You'll Also Like:

  • 1.  jQuery keyup Method
  • 2.  Using jQuery to Change Background Color
  • 3.  Get the Window Scroll Position Using jQuery
  • 4.  jQuery mouseout – An Interactive Example of the mouseout Method
  • 5.  jQuery blur – How to Use the blur() Method on an HTML Form
  • 6.  Get Selected Option from Dropdown Using jQuery
  • 7.  Get the Height of a Div Using jQuery
  • 8.  Using jQuery to Get Element by ID
  • 9.  Using jQuery to Show and Hide a Div
  • 10.  How to Clear an Input Field Using jQuery

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