• 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 / JavaScript / JavaScript onfocus – Changing Form Styles with the onfocus Event

JavaScript onfocus – Changing Form Styles with the onfocus Event

March 6, 2022 Leave a Comment

The JavaScript onfocus event 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 type="text" onfocus="someFunction()">

We can add a function call to the onfocus event and run some code when a user’s focus enters the input.


Let’s say we have the following HTML:

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

To highlight the input field when the user clicks on it, we can add an onfocus event to the input field.

Once the user clicks on the input field, the onfocus event will be triggered and will launch the changeBackground() function where we can change its background color.

function changeBackground(){
  document.getElementById("fname").style.backgroundColor = "green";
};

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

An Interactive Example of Using JavaScript with the onfocus Event

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" onfocus="changeBackground()" onfocusout="resetBackground()">
  <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 onfocusout event to run the resetBackground() function where we can reset the background color.

In the resetBackground() function, we will change the background color of the input field by using the backgroundColor property.

Here are the code JavaScript functions:

function changeBackground(){
  document.getElementById("fname").style.backgroundColor = "#d1e2f3";
};

function resetBackground(){
  document.getElementById("fname").style.backgroundColor = "#fff";
};

The final code and output for this example of using JavaScript with the onfocus event is below:

Code Output:




Full Code:

<form>
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname" onfocus="changeBackground()" onfocusout="resetBackground()">
  <button type="submit" value="Submit">Submit</button>
</form>

<script>

function changeBackground(){
  document.getElementById("fname").style.backgroundColor = "#d1e2f3";
};
function resetBackground(){
  document.getElementById("fname").style.backgroundColor = "#fff";
};

</script>

Hopefully this article has been useful to help you understand how to use JavaScript with the onfocus event.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Show a Div
  • 2.  Add Years to a Date Using JavaScript
  • 3.  Using JavaScript to Replace Space With Dash
  • 4.  Subtract All Numbers in an Array Using JavaScript
  • 5.  JavaScript onkeydown – How to Use onkeydown Event with JavaScript
  • 6.  innerHTML vs outerHTML
  • 7.  How to Clear a Textarea Field In JavaScript
  • 8.  Using JavaScript to Resize an Image
  • 9.  Using JavaScript to get Textarea Value
  • 10.  JavaScript Change Text Color of a Paragraph

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