Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HTML DOM Keygen autofocus Property

  • Last Updated : 15 Nov, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

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.

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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!