Javascript Reference - HTML DOM Input Date autofocus Property








The autofocus property sets or gets whether a date field can automatically get focus when the page loads.

Browser Support

autofocus Yes No No Yes Yes

Syntax

Return the autofocus property.

var v = inputdateObject.autofocus 

Set the autofocus property.

inputdateObject.autofocus=true|false

Property Values

Value Description
true|false Set whether a date field can get focus when the page loads
  • true - The date field gets focus
  • false - Default. The date field does not get focus




Return Value

A Boolean type value, true if the date field automatically gets focus when the page loads, otherwise false.

Example

The following code shows how to get if a date field automatically gets focus upon page load.


<!DOCTYPE html>
<html>
<body>
<!--from w w  w.  j a  va 2s . c o m-->
<input type="date" id="myDate" autofocus>
<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    var x = document.getElementById("myDate").autofocus;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: