Javascript DOM HTML Input DatetimeLocal autofocus Property

Introduction

Find out if a local datetime field automatically gets focus on page load:

var x = document.getElementById("myLocalDate").autofocus;

Click button to find out if the local datetime field automatically gets focus on page load.

View in separate window

<!DOCTYPE html>
<html>
<body>

<input type="datetime-local" id="myLocalDate" autofocus>

<button onclick="myFunction()">Test</button>
<p id="demo"></p>

<script>
function myFunction() {//from  ww w  .j  a v a 2s .c  o m
  var x = document.getElementById("myLocalDate").autofocus;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The autofocus property sets or gets whether a local datetime field should automatically get focus on page load.

This property mirrors the HTML autofocus attribute.

The autofocus property accepts and returns a boolean type value.

Value Description
true The local datetime field gets focus
falseDefault. The local datetime field does not get focus

The autofocus property returns true if the local datetime field automatically gets focus when the page loads, otherwise it returns false.




PreviousNext

Related