• 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 focusout – How to Use the focusout() Method on an HTML Form

jQuery focusout – How to Use the focusout() Method on an HTML Form

February 10, 2022 Leave a Comment

The jQuery focusout method 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").focusout(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 get the value that the user has entered for “Full Name”, we could use the jQuery focusout() method along with the val() method.

Once the user has typed a name and leaves the input field, the focusout method will be triggered.

$("#fname").focusout(function(){
  var inputVal = $("#fname").val();
});

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

jQuery("#fname").focusout(function(){
  var inputVal = jQuery("#fname").val();
});

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

An Interactive Example of the jQuery focusout() Method

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">
  <button type="submit" value="Submit" disabled>Submit</button>
</form>

We will then use jQuery 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 display a message using the insertAfter() method.

Finally, we will use the focusin() method to clear any previous warning messages so they don’t stack up.

Here is the code:

$("#fname").focusout(function(){
  var inputVal = $("#fname").val();
  if( inputVal.length != 0 ){
    //The name field is NOT empty
    $('button').prop('disabled', false);
  } else {
    //The name field IS empty
    $('button').prop('disabled', true);
    $('<p id="no-name" style="color:red;font-size:14px;">Make sure to enter a name!</p>').insertAfter("#fname");
  } 
});

$("#fname").focusin(function(){
  $('#no-name').remove();
});

The final code and output for this example is below.

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

Code Output:




Full Code:

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

<script>

$("#fname").focusout(function(){
  var inputVal = $("#fname").val();
  if( inputVal.length != 0 ){
    //The name field is NOT empty
    $('button').prop('disabled', false);
  } else {
    //The name field IS empty
    $('button').prop('disabled', true);
    $('<p id="no-name" style="color:red;font-size:14px;">Make sure to enter a name!</p>').insertAfter("#fname");
  } 
});

$("#fname").focusin(function(){
  $('#no-name').remove();
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Top Position of Element
  • 2.  Using jQuery to Change Label Text
  • 3.  Using jQuery to Change the Font Size of a Paragraph
  • 4.  Set the Height of a Div Using jQuery
  • 5.  Using jQuery to Select Multiple Classes
  • 6.  jQuery insertBefore – Insert Element Before Another Element
  • 7.  Using jQuery to see if URL Contains Specific Text
  • 8.  Toggle the Visibility of an Element Using jQuery
  • 9.  jQuery mousedown – An Interactive Example of the jQuery mousedown Method
  • 10.  How to Use jQuery to Remove 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