HTML DOM Input Range autofocus property


The HTML DOM input range autofocus property is associated with the HTML <input> element’s autofocus attribute. This property is used for setting or returning if the input range slider should be automatically focused when the page loads or not.

Syntax

Following is the syntax for −

Setting the autofocus property −

rangeObject.autofocus = true|false

Here, true represents the range slider should get focus and the false represents otherwise. It is set to false by default.

Example

Let us look at an example for the Input range autofocus property −

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h1>Input range autofocus property</h1>
<form>
<input type="range" id="RANGE1" name="VOL" autofocus>
</form>
<br>
<button type=”button” onclick="rangeFocus()">CHECK</button>
<p id="Sample"></p>
<script>
   function rangeFocus() {
      var R = document.getElementById("RANGE1").autofocus;
      document.getElementById("Sample").innerHTML = "The autofocus property is set to: "+R;
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the CHECK button −

In the above example −

We have created an input field contained inside a form having type=“range” , id=“RANGE1” ,name=“VOL” and it has the autofocus property enabled i.e set to true.

<form>
<input type="range" id="RANGE1" name="VOL" autofocus>
</form>

We have then created a button CHECK that will execute the rangeFocus() method on being clicked by the user −

<button type=”button” onclick="rangeFocus()">CHECK</button>

The rangeFocus() method gets the input element with type range using the getElementById() method and gets autofocus property. The autofocus property returns true and false depending on the range slider autofocus attribute value. This value is assigned to variable R and displayed in the paragraph with id “Sample” using its innerHTML property −

function rangeFocus() {
   var R = document.getElementById("RANGE1").autofocus;
   document.getElementById("Sample").innerHTML = "The autofocus property is set to: "+R;
}

Updated on: 22-Aug-2019

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements