HTML | DOM onfocusout Event

The HTML DOM onfocusout event occurs when an element is losing focus. The onfocusout is same as onblur, the only difference is that onblur event does not bubble.

If you want to find out whether an element or its child loses the focus, you should use the onfocusout event.
The onfocusin event is the opposite of the onfocusout event.

Note: Firefox does not support the onfocusout event but with the help of capturing listener you can find out whether a child of an element loses the focus or not.
Syntax:



In HTML:

<element onfocusout="myScript">

In JavaScript (may not work as expected in Chrome, Safari and Opera 15+):

object.onfocusout = function(){myScript};

In JavaScript, using the addEventListener() method:

object.addEventListener("focusout", myScript);

Example: Using HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <h2>HTML DOM onfocusout event </h2> Email:
        <input type="email" id="email" onfocusout="gfgFun()">
  
        <script>
            function gfgFun() {
                var x = document.getElementById("email");
                alert("Focus out");
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

Example: Using JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
  
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <h2>HTML DOM onfocusout event </h2> Email:
        <input type="email" id="email">
  
        <script>
            document.getElementById(
              "email").onfocusout = function() {
                gfgFun()
            };
  
            function gfgFun() {
                alert("Focus out");
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:


Example: Using the addEventListener() method:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <h2>HTML DOM onfocusout event </h2
      Email: <input type="email" id="email">
  
        <script>
            document.getElementById(
              "email").addEventListener("focusout", gfgFun);
  
            function gfgFun() {
                alert("Input field lost focus.");
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:


Supported Browsers: The browsers supported by HTML DOM onfocusout Event are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox 52.0
  • Apple Safari
  • Opera


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.