Open In App

HTML DOM Input Button autofocus Property

Last Updated : 02 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Input Button autofocus Property in HTML DOM is used to set or return whether the Input Button field should automatically get focused 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 used 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>
    <title>
        HTML DOM Input Button autofocus Property
    </title>
</head>
 
<body style="text-align:center;">
    <h1>GeeksforGeeks</h1>
 
    <h2>HTML 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="result"></p>
 
    <script>
        function myGeeks() {
 
            // Return Input Button autofocus Property
            let btnFocus = document.getElementById("GFG").autofocus;
            document.getElementById("result").innerHTML = btnFocus;
        }
    </script>
</body>
 
</html>


Output: 

input-button-autofocus

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

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM Input Button autofocus Property
    </title>
</head>
 
<body style="text-align:center;">
    <h1>GeeksforGeeks</h1>
 
    <h2>HTML 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="result"></p>
 
    <script>
        function myGeeks() {
 
            // Setting Input Button autofocus Property
            let btnFocus = document.getElementById("GFG")
                .autofocus = false;
 
            document.getElementById("result").innerHTML
                = btnFocus;
        }
    </script>
</body>
 
</html>


Output:

input-button-autofocus-2

Supported Browsers:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads