• 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
  • VBA
  • 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.  Input Mask Phone Number Using jQuery
  • 2.  How to Set Checkbox Checked With jQuery
  • 3.  jQuery mousedown – An Interactive Example of the jQuery mousedown Method
  • 4.  jQuery prev – Get the Previous Element of a Div
  • 5.  jQuery mouseover – An Interactive Example of the mouseover Method
  • 6.  jQuery first() – Get the First Element
  • 7.  jQuery keyup Method
  • 8.  Using jQuery to Get the Margin of an Element
  • 9.  Get the Height of a Div Using jQuery
  • 10.  Resizing an Image 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy