HTML DOM Input Radio autofocus property

HTMLWeb DevelopmentFront End Technology

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

Syntax

Following is the syntax for −

Setting the autofocus property.

radioObject.autofocus = true|false

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

Example

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

<!DOCTYPE html>
<html>
<body>
<h1>Input password autofocus property</h1>
<form>
RADIO: <input type="radio" id="rad1" name=”BTN” autofocus>
</form>
<br><br>
<button type=”button” onclick="FocusVal()">CHECK FOCUS</button>
<p id="Sample"></p>
<script>
   function FocusVal() {
      var R = document.getElementById("rad1").autofocus;
      document.getElementById("Sample").innerHTML = "The radio button has autofocus property
      set to "+R;
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the CHECK FOCUS button −

In the above example −

We have created an input field with type=“radio”, id=“rad1”, name=“BTN” and it has the autofocus property enabled i.e set to true −

Password: <input type="password" id="rad1" name=”BTN”autofocus>

We have then created a button CHECK FOCUS that will execute the FocusVal() method when clicked by the user −

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

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

function FocusVal() {
   var R = document.getElementById("rad1").autofocus;
   document.getElementById("Sample").innerHTML = "The radio button has autofocus property set to "+R;
}
raja
Updated on 21-Aug-2019 06:42:59

Advertisements