• 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 onfocusout – How to Use the onfocusout Event on an HTML Form

JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form

March 6, 2022 Leave a Comment

The JavaScript onfocusout event will occur when an element (most of the time an input) loses the user’s focus. This occurs when either the user mouse clicks away from that element, or tabs away from it using the keyboard.

<input type="text" onfocusout="someFunction()">

Let’s say we have the following HTML:

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

To get the value that the user has entered for “Full Name”, we could use JavaScript along with the onfocusout event.

Once the user has typed a name and leaves the input field, the onfocusout event will be triggered, which will call the getValue() function. The function will use the value property to get the name entered by the user.

function getValue(){
  var inputVal = document.getElementById("fname").value;
});

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

An Interactive Example of Using JavaScript with the onfocusout Event

Below we will have a simple form with a name field and a submit button. One thing that is common in a lot of online forms is making sure actual information is entered before allowing the user to submit a form.

So below we will have a form that only lets you submit the information if you put in anything in the name field. If the user enters the name field but does not enter anything, we will display a warning to them. Here is the HTML code:

<form>
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname" onfocusout="getName()" onfocus="clearMessages()">
  <p id="no-name" style="color:red;font-size:14px;display:none;">Make sure to enter a name!</p>
  <button id="button1" type="submit" value="Submit" disabled>Submit</button>
</form>

We will then use the disabled property to disable or enable the submit button based on whether there is a string in the name field. We will do this using the length property. If the field is empty we will show a hidden “p” element by changing its display property.

Finally, we will use the onfocus event to clear any previous warning messages so they don’t stack up.

Here is the code:

function getName(){
  var inputVal = document.getElementById("fname").value;
  if( inputVal.length != 0 ){
    //The name field is NOT empty
    document.getElementById("button1").disabled = false;
  } else {
    //The name field IS empty
    document.getElementById("button1").disabled = true;
    //Show the error message
    document.getElementById("no-name").style.display = "block";
  } 
};

function clearMessages(){
  //Hide the error message
  document.getElementById("no-name").style.display = "none";
};

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

Make sure to click the form field and click away with nothing in it to see the full example.

Code Output:


Make sure to enter a name!


Full Code:

<form>
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname" onfocusout="getName()" onfocus="clearMessages()">
  <p id="no-name" style="color:red;font-size:14px;display:none;">Make sure to enter a name!</p>
  <button id="button1" type="submit" value="Submit" disabled>Submit</button>
</form>

<script>

function getName(){
  var inputVal = document.getElementById("fname").value;
  if( inputVal.length != 0 ){
    document.getElementById("button1").disabled = false;
  } else {
    document.getElementById("button1").disabled = true;
    document.getElementById("no-name").style.display = "block";
  } 
};

function clearMessages(){
  document.getElementById("no-name").style.display = "none";
};

</script>

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

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Change the href of a Link
  • 2.  Using JavaScript to Remove Substring From String
  • 3.  How to Iterate through an Array Using JavaScript
  • 4.  Using JavaScript to Get a Random Number Between 1 and 10
  • 5.  How to Check if a String Contains Vowels in JavaScript
  • 6.  Using JavaScript to Get the Length of Array
  • 7.  Using JavaScript to Replace a Character at an Index
  • 8.  Using Javascript to Check if Value is Not Null
  • 9.  Using Javascript to Remove the Id from a Div
  • 10.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript

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