Javascript DOM HTML Select autofocus Property

Introduction

Find out if a drop-down list automatically gets focus on page load:

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

Click the button to find out if the drop-down list automatically gets focus on page load.

View in separate window

<!DOCTYPE html>
<html>
<body>

<select id="mySelect" autofocus>
  <option value="javascript">Javascript</option>
  <option value="html">HTML</option>
  <option value="opel">Opel</option>
  <option value="sql">SQL</option>
</select>//from   w  w  w.  j  a  va  2 s  .c  o  m
<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

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

The autofocus property returns true if the drop-down list automatically gets focus on page load, otherwise it returns false.




PreviousNext

Related