HTML DOM Input Button autofocus Property

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>w3wiki</h1>
 
    <h2>HTML DOM Input Button autofocus Property</h2>
     
    <form id="myBeginner">
     
        <!-- Assigning button id -->
        <input type="button" id="GFG"
            onclick="myBeginner()" name="Geek_button"
            value="Submit" autofocus>
    </form>
 
    <p id="result"></p>
 
    <script>
        function myBeginner() {
 
            // Return Input Button autofocus Property
            let btnFocus = document.getElementById("GFG").autofocus;
            document.getElementById("result").innerHTML = btnFocus;
        }
    </script>
</body>
 
</html>


Output: 

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>w3wiki</h1>
 
    <h2>HTML DOM Input Button autofocus Property</h2>
     
    <form id="myBeginner">
     
        <!-- Assigning button id -->
        <input type="button" id="GFG"
            onclick="myBeginner()" name="Geek_button"
            value="Submit" autofocus>
    </form>
 
    <p id="result"></p>
 
    <script>
        function myBeginner() {
 
            // Setting Input Button autofocus Property
            let btnFocus = document.getElementById("GFG")
                .autofocus = false;
 
            document.getElementById("result").innerHTML
                = btnFocus;
        }
    </script>
</body>
 
</html>


Output:

Supported Browsers:

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


Contact Us