The Wayback Machine - https://web.archive.org/web/20250113232415/https://www.geeksforgeeks.org/implement-a-javascript-when-an-element-loses-focus/
Open In App

Implement a JavaScript when an element loses focus

Last Updated : 19 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a document, the task is to implement functionality when the element loses focus. We have 2 options, one is the onblur event and another is onfocusout event JavaScript. We’re going to discuss a few methods. First few methods to understand.

Approach 1: Using onblur event

JavaScript onblur Event: This event happens when an element is going to lose focus.

  • In HTML
    <element onfocusout="script">
  • In JavaScript
    object.onfocusout = function(){script};
  • In JavaScript, with the addEventListener() method
    object.addEventListener("blur", script);

Example 1: This example adds an onblur event to the <input> element and when it happens the specified code runs.

html




<h1 style="color:green;">
    GeeksforGeeks
</h1>
<p id="GFG_UP">
</p>
<input type="text" name="input" value="inputElement" onblur="gfg_Run();" />
<p id="GFG_DOWN">
</p>
<script>
    var el_up = document.getElementById("GFG_UP");
    var el_down = document.getElementById("GFG_DOWN");
    var today = 'First click inside of input'+
    ' and then outside to perform event!';
    el_up.innerHTML = today;
      
    function gfg_Run() {
        el_down.innerHTML = "Input element lost focus";
    }
</script>


Output:

Implement a JavaScript when an element loses focus

Implement a JavaScript when an element loses focus

Approach 2: Using onfocusout event

JavaScript onfocusout Event: This method appends a node as the last child of a node.

Syntax:

  • In HTML
    <element onfocusout="script">
  • In JavaScript
    object.onfocusout = function(){script};
  • In JavaScript, with the addEventListener() method
    object.addEventListener("focusout", script);

Example 2: This example adds an onfocusout event to the <input> element and when it happens the specified code runs.

html




<h1 style="color:green;">
    GeeksforGeeks
</h1>
<p id="GFG_UP">
</p>
<input type="text" name="input" value="inputElement" onfocusout="gfg_Run();" />
<p id="GFG_DOWN">
</p>
<script>
    var el_up = document.getElementById("GFG_UP");
    var el_down = document.getElementById("GFG_DOWN");
    var today = 'First click inside of input'+
        ' and then outside to perform event!';
    el_up.innerHTML = today;
      
    function gfg_Run() {
        el_down.innerHTML = "Input element lost focus";
    }
</script>


Output:

Implement a JavaScript when an element loses focus

Implement a JavaScript when an element loses focus


Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


Similar Reads

three90RightbarBannerImg