Open In App

HTML | DOM FocusEvent

The DOM FocusEvent Object contains the events that are related to focus. It includes events like focus in, focus out and blur. 

Properties:



Example: Finding out related event with the relatedTarget property. 




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM FocusEvent</title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
 
    <b>DOM FocusEvent</b>
     
<p>
        The relatedTarget property will
        return the element that will
        return the secondary target.
    </p>
 
     
     
<p>Textarea with id of "text1"</p>
 
     
    <textarea id="text1"
        onfocus="getRelatedTarget()">
    </textarea>
     
     
<p>Textarea with id of "text2"</p>
 
     
    <textarea id="text2"></textarea>
     
    <script>
        function getRelatedTarget() {
            console.log(this.event.relatedTarget);
        }
    </script>
</body>
 
</html>

Output:



 

 

Event Types:

Example: This example implements the onfocusin event. 




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM FocusEvent</title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
 
    <b>DOM FocusEvent</b>
     
     
<p>
        The onfocusin event fires whenever an
        element is about to receive focus.
    </p>
 
     
    <textarea id="text1" onfocusin="fireEvent()">
    </textarea>
     
    <script>
        function fireEvent() {
            console.log("The textarea was focused.");
        }
    </script>
</body>
 
</html>

Output:

 

 

Example: This example implements the onfocusout event. 




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM FocusEvent</title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
 
    <b>DOM FocusEvent</b>
     
     
<p>
        The onfocusout event fires whenever an
        element is about to lose focus.
    </p>
 
     
    <textarea id="text1" onfocusout="fireEvent()">
    </textarea>
     
    <script>
        function fireEvent() {
            console.log("The textarea was unfocused.");
        }
    </script>
</body>
 
</html>

Output:

 

 

Supported Browsers: The browser supported by FocusEvent Object are listed below:


Article Tags :