HTML DOM Input Radio autofocus property

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.


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.


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

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


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;

Updated on: 21-Aug-2019


Kickstart Your Career

Get certified by completing the course

Get Started