Javascript Reference - HTML DOM Input Datetime autofocus Property








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

Browser Support

autofocus No No No No No

Syntax

Return the autofocus property.

var v = datetimeObject.autofocus 

Set the autofocus property.

datetimeObject.autofocus=true|false

Property Values

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




Return Value

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

Example

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


<!DOCTYPE html>
<html>
<body>
<input type="datetime" id="myDatetime" autofocus>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from  w w  w.j ava  2  s .c o m-->
    var x = document.getElementById("myDatetime").autofocus;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows: