Skip to content
Related Articles
Open in App
Not now

Related Articles

HTML | DOM Input Button autofocus Property

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 18 Aug, 2022
Improve Article
Save Article
Like Article

The DOM Input Button autofocus Property in HTML DOM is used to set or return whether the Input Button field should get automatically get focus or not when the page loads. This Property is used to reflect the HTML autofocus attribute. 

Syntax: 

  • It returns the autofocus property.
buttonObject.autofocus
  • It is use to set the autofocus property.
buttonObject.autofocus = true|false

Property Values: 

  • true: It specify that the Button field get focus.
  • false: It specify that the Button field does not get focus. It has a default value.

Return Value: It returns a Boolean value which represents that the Button field get focus or not when the page loads. 

Example 1: This Example illustrates how to return the autofocus Property. 

HTML




<!DOCTYPE html>
<html>
<head>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
<body style="text-align:center;">
    <h1>GeeksForGeeks</h1>
    <h2> DOM Input Button autofocus Property </h2>
        <form id="myGeeks">
    <!-- Assigning button id -->
    <input type="button" id="GFG" onclick="myGeeks()"
    name="Geek_button" value="Submit" autofocus>
                    </form>
    <p id="sudo" style="color:green;font-size:25px;"></p>
 
    <script>
        function myGeeks() {
             
            // Return Input Button autofocus Property
            var g = document.getElementById("GFG").autofocus;
            document.getElementById("sudo").innerHTML = g;
        }
    </script>
</body>
</html>

Output: 

Before Clicking On Button:

  

After Clicking On Button: 

 

Example 2: This Example illustrates how to set the autofocus Property. 

HTML




<!DOCTYPE html>
<html>
<head>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
<body style="text-align:center;">
    <h1>GeeksForGeeks</h1>
    <h2> DOM Input Button autofocus Property </h2>
        <form id="myGeeks">
    <!-- Assigning button id -->
    <input type="button" id="GFG" onclick="myGeeks()"
     name="Geek_button" value="Submit" autofocus>
                    </form>
    <p id="sudo" style="color:green;font-size:25px;"></p>
 
    <script>
        function myGeeks() {
             
            // Setting Input Button autofocus Property
            var g = document.getElementById("GFG").autofocus = false;
            document.getElementById("sudo").innerHTML = g;
        }
    </script>
</body>
</html>

Output:

Before Clicking On Button: 

 

After Clicking On button:

  

Supported Browsers: The browser supported by DOM input Button autofocus property are listed below:

  • Google Chrome
  • Internet Explorer 10.0 +
  • Firefox
  • Opera
  • Safari

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!