Open In App

HTML | DOM Input image autofocus Property

Last Updated : 26 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The HTML DOM input Image autofocus Property is used to set or return whether the Input Image Field should get focus or not when the page loading. It reflects the HTML autofocus attribute. 

Syntax: 

  • It returns the autofocus property.
imageObject.autofocus
  • It is used to set the autofocus property.
imageObject.autofocus = "true|false"

Property values:

  • true: It sets the focus of Input Image.
  • false: It is the default value. It defines the Input Image field does not get focus.

Return value: It returns the boolean value which represents the image field gets autofocus or not. 

Example 1: This example returns the Input Image autofocus property. 

HTML




<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Input Image autofocus Property
    </title>
</head>
<body style="text-align:center;">
    <h1 style="color:green;">
      GeeksforGeeks
    </h1>
    <h4>
      DOM Input Image autofocus Property
    </h4>
    <input id="myImage" formEnctype="application/x-www-form-urlencoded"
           type="image" formtarget="#" src=
           alt="Submit" width="48" height="48" formMethod="post"
           autofocus>
    <br>
    <br>
    <button onclick="my_geek()">Submit </button>
    <h2 id="Geek_h" style="color:green;">
    </h2>
    <script>
        function my_geek() {
 
            // return autofocus Property.
            var txt = document.getElementById(
                "myImage").autofocus;
            document.getElementById(
                "Geek_h").innerHTML = txt;
        }
    </script>
</body>
</html>


Output:

  • Before Clicking On Button:

  • After Clicking On Button:

Example 2: This example sets the Input Image autofocus property. 

HTML




<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Input Image autofocus Property
    </title>
</head>
<body style="text-align:center;">
    <h1 style="color:green;">
            GeeksforGeeks
    </h1>
    <h4>DOM Input Image autofocus Property</h4>
    <input id="myImage" formEnctype="application/x-www-form-urlencoded"
           type="image" formtarget="#" src=
           alt="Submit" width="48" height="48" formMethod="post"
           autofocus>
    <br>
    <br>
    <button onclick="my_geek()">Submit </button>
    <h2 id="Geek_h" style="color:green;">
    </h2>
    <script>
        function my_geek() {
 
            // set autofocus Property.
            var txt = document.getElementById(
                "myImage").autofocus = false;
            document.getElementById(
                "Geek_h").innerHTML = txt;
        }
    </script>
</body>
</html>


Output:

  • Before Clicking On Button:

 

  • After Clicking On Button:

 

Supported Browsers: The browsers supported by HTML DOM Input image autofocus Property are listed below:

  • Google Chrome 
  • Edge 12+
  • Firefox 
  • Opera 
  • Safari 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads