The Wayback Machine - https://web.archive.org/web/20250223182208/https://www.geeksforgeeks.org/html-onblur-attribute/
Open In App

HTML onblur Attribute

Last Updated : 29 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

This attribute fires at that moment when the element loses focus. This attribute is mostly used in the Form validation code. This attribute is the opposite of onfocus attribute.
Supported Tags: It supports all HTML elements except these elements which are given below : 

  • <base>
  •  <bdo>
  •  <br>
  •  <head>
  •  <html>
  •  <iframe>
  •  <meta>
  •  <param>
  •  <script>
  •  <style>
  •  <title>

Syntax: 

<element onblur = "script">

Attribute Value: This attribute is used to any HTML elements. The script run when onblur attribute called.

Example: In this example we use the onblur attribute to call a JavaScript function that converts the input text to uppercase when the user leaves the input field.

html
<!DOCTYPE html>
<html>

<head>
    <title>onblur attribute</title>
    <style>
        body {
            text-align: center;
        }

        h1 {
            color: green;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>onblur attribute</h2>

    <!-- When leave the input box then the input box 
        value is converted into capital letter. -->
    Username: <input type="text" 
                     name="fname" 
                     id="GFG" 
                     onblur="Geeks()">
    <script>
        function Geeks() {
            let y = document.getElementById("GFG");
            y.value = y.value.toUpperCase();
        }
    </script>
</body>

</html>

Output: 
 


Supported Browsers: The browser supported by onblur attribute are listed below: 

HTML onblur Attribute – FAQs

Which HTML elements can use the onblur attribute?

The onblur attribute can be used with form elements like <input>, <textarea>, <select>, <button>, and other interactive elements like <a>.

What is the difference between onblur and onfocus?

The onblur attribute triggers when an element loses focus, while the onfocus attribute triggers when an element gains focus (e.g., when a user clicks or tabs into it).

Can the onblur attribute be used to validate form fields?

Yes, the onblur attribute is commonly used to perform form field validation by triggering a JavaScript function to check the input when the user leaves the field.

Can the onblur attribute be used to trigger an AJAX call?

Yes, the onblur attribute can be used to trigger an AJAX call by calling a JavaScript function that performs the AJAX request when the element loses focus.

Is the onblur attribute case-sensitive?

While HTML attributes are generally not case-sensitive, it is best practice to use lowercase (onblur) to ensure compatibility and readability.
 


Next Article

Similar Reads

three90RightbarBannerImg