Table of contents
1.
Introduction
2.
What is onblur?
2.1.
Syntax of JavaScript onblur
2.2.
Parameters of JavaScript onblur
2.3.
Exceptions of JavaScript onblur
2.4.
Example
3.
Working with onblur
4.
Event Object and onblur
5.
Frequently Asked Questions
5.1.
How to detect onfocus in JavaScript?
5.2.
What is the difference between onfocus and onblur?
5.3.
What is the lexer in JavaScript?
6.
Conclusion
Last Updated: Jan 22, 2025
Easy

JavaScript Onblur

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

As a web developer, responding to user interactions is a big part of your job. It's not all about clicking or typing; sometimes, it's about what happens when the user stops interacting. JavaScript's onblur event can help us handle these scenarios. This article will explain what onblur is, how to use it, and provide some real-world examples.

JavaScript Onblur

What is onblur?

The onblur event in JavaScript occurs when an element loses focus. It is commonly used with form elements like input, textarea, and select to trigger validation or perform actions when the user navigates away from the element.

Syntax of JavaScript onblur

element.onblur = function;

Parameters of JavaScript onblur

function(event)

Exceptions of JavaScript onblur

No exceptions are there in JavaScript onblur.

In the syntax for the onblur event in JavaScript, you assign a function to be executed when an element loses focus. The function specified as the event handler will be invoked when the blur event occurs on the element.

The parameter of the onblur event handler function is typically an event object, which provides information about the event that occurred. This event object can be used to access properties and methods related to the blur event.

Example

Here's a simple example of onblur in action:

document.getElementById("nameInput").onblur = function() { 
    console.log('Input field lost focus'); 
}
You can also try this code with Online Javascript Compiler
Run Code

 

In this snippet, when the user clicks or tabs away from the input field with the ID nameInput, 'Input field lost focus' is logged to the console.

Working with onblur

One of the most common uses of onblur is to validate user input as soon as they finish typing. This provides immediate feedback, which can be helpful to the user. Let's see how you could use onblur to validate an email field:

document.getElementById("emailInput").onblur = function() {
    let email = this.value;
    let pattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/;


    if (email.match(pattern)) {
        console.log('Valid email');
    } else {
        console.log('Invalid email');
    }
}
You can also try this code with Online Javascript Compiler
Run Code

 

Here, when the user finishes typing their email and clicks or tabs away from the input field, the email is checked against a regular expression pattern. If the email is valid, 'Valid email' is logged to the console. If it's not, 'Invalid email' is logged.

Event Object and onblur

Just like any other event in JavaScript, onblur also generates an event object that holds information about the event. While it's not as commonly used with onblur as it is with other events, you can still access this object if needed.

document.getElementById("nameInput").onblur = function(event) {
    console.log('Element that lost focus: ' + event.target.id);
}
You can also try this code with Online Javascript Compiler
Run Code

 

In this example, event.target.id is used to log the ID of the element that lost focus.

Frequently Asked Questions

How to detect onfocus in JavaScript?

Use the onfocus event to detect when an element gains focus. It can be applied inline, through JavaScript, or with event listeners.

What is the difference between onfocus and onblur?

onfocus triggers when an element gains focus, while onblur triggers when an element loses focus. Both are commonly used for form interactions.

What is the lexer in JavaScript?

A lexer (lexical analyzer) in JavaScript breaks the source code into tokens, which are the smallest meaningful units, for parsing and execution.

Conclusion

JavaScript's onblur event is an important tool in the toolkit of every web developer. It allows you to react when an element loses focus, enabling you to create more interactive and user-friendly web pages. Whether it's validating user input, saving data, or triggering animations, onblur offers a wealth of possibilities to enhance your site's usability. By understanding and effectively using onblur, you can take your user interactions to the next level.

Live masterclass