Input Button autofocus Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Button


The autofocus property sets or gets whether an input button should get focus when the page loads.

This property reflects the HTML autofocus attribute.

Set the autofocus property with the following Values

Value Description
true|false Sets whether an input button should get focus when the page loads
  • true - The input button gets focus
  • false - Default. The input button does not get focus

Return Value

A Boolean, returns true if the input button gets focus when the page loads, otherwise false

The following code shows how to check if a button automatically gets focus upon page load:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<input type="button" id="myBtn" autofocus value="Input Button">

<p>Click the button below to find out if the input button above automatically gets focus when the page loads.</p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

function myFunction() {//from   w  w  w. j av  a2s  .c  om
    var x = document.getElementById("myBtn").autofocus;
    document.getElementById("demo").innerHTML = x;


Related Tutorials