Javascript Reference - HTML DOM Input Search autofocus Property








The autofocus property sets or gets whether a search field should auto focus when the page loads.

Browser Support

autofocus Yes 10.0 Yes Yes Yes

Syntax

Return the autofocus property.

var v = searchObject.autofocus 

Set the autofocus property.

searchObject.autofocus=true|false

Property Values

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




Return Value

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

Example

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


<!DOCTYPE html>
<html>
<body>
<!--from   w w  w.  jav a2s.co m-->
Search: <input type="search" id="mySearch" autofocus>
<button onclick="myFunction()">test</button>

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

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

</body>
</html>

The code above is rendered as follows: