Javascript DOM HTML Input Submit autofocus Property get

Introduction

Find out if a Submit button automatically gets focus on page load:

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

Click the button to find out if the Submit button automatically gets focus on page load.

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="submit" id="mySubmit" value="Submit" autofocus>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*from w  ww  . j  av  a2s  .  c om*/
  var x = document.getElementById("mySubmit").autofocus;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The autofocus property sets or gets whether a submit button 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 submit button gets focus
falseDefault. The submit button does not get focus

The autofocus property returns true if the submit button automatically gets focus on page load, otherwise it returns false.




PreviousNext

Related