Open In App

HTML DOM Keygen autofocus Property

Last Updated : 13 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The keygen autofocus property in HTML DOM is used to set or return the value of the autofocus attribute of an <keygen> element. The autofocus attribute is used to define whether the keygen element gets automatically focused or not when the page loads or a refresh takes place.

Syntax:

Returns the autofocus property.

keygenObject.autofocus

Sets the autofocus property.

keygenObject.autofocus = true|false

Property Values:

  • true|false: It is used to specify whether a keygen element should get focus when the page loads or refreshes. It is false by default.

Return Values: It returns a Boolean value that represents whether the keygen element has to be focused or not. 

Example 1: The below example illustrates how to return the autofocus property. 

HTML




<!DOCTYPE html>
<html>
   
<head>
    <style>
        h1 {
            color: green;
        }
         
        h2 {
            font-family: Impact;
        }
         
        body {
            text-align: center;
        }
    </style>
</head>
   
<body>
    <h1>GeeksforGeeks</h1>
    <h2>HTML DOM Keygen autofocus Property</h2>
    <br>
    <form id="myGeeks">
        Username: <input type="text" name="uname">
        <br><br> Encryption:
        <keygen id = "Geeks"form="myGeeks"
                name="secure" autofocus>
        <input type="submit">
    </form>
 
     
 
<p>
          To find out whether the keygen element
        automatically gets focus on page load
          or ot, click the "Check" button.
      </p>
 
 
 
    <button onclick="My_focus()">Check</button>
    <p id="test"></p>
 
 
   
    <script>
        function My_focus() {
            var d = document.getElementById("Geeks").autofocus;
            document.getElementById("test").innerHTML = d;
        }
    </script>
</body>
   
</html>


Output:

Example 2: The below example demonstrates how to set the autofocus property. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <style>
        h1 {
            color: green;
        }
 
        h2 {
            font-family: Impact;
        }
 
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>HTML DOM Keygen autofocus Property</h2>
    <br>
 
    <form id="myGeeks">
        Username: <input type="text" name="uname">
        <br><br> Encryption:
        <keygen id="Geeks" form="myGeeks"
            name="secure" autofocus>
        <input type="submit">
    </form>
     
     
 
<p>
        To set the autofocus property,
        click on "set" Button.
    </p>
 
 
    
    <button onclick="set_focus()">set</button>
     
    <p id="test"></p>
 
 
     
    <script>
        function set_focus() {
            var d = document.getElementById(
                    "Geeks").autofocus = "false";
                     
            document.getElementById("test").innerHTML = d;
        }
    </script>
</body>
 
</html>


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads