Javascript DOM HTML Input Search autofocus Property get

Introduction

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

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

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

View in separate window

<!DOCTYPE html>
<html>
<body>

Search: <input type="search" id="mySearch" autofocus>

<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {// w  ww.  j a  v a2  s  .  co  m
  var x = document.getElementById("mySearch").autofocus;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The autofocus property sets or gets whether a search 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.

Property Values

Value Description
true The search field gets focus
false Default. The search field does not get focus

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




PreviousNext

Related