Javascript Reference - HTML DOM Input URL autofocus Property








The autofocus property sets or gets whether a URL field can automatically get focus when the page loads.

Browser Support

autofocus Yes 10.0 Yes Yes Yes

Syntax

Return the autofocus property.

var v = urlObject.autofocus 

Set the autofocus property.

urlObject.autofocus=true|false

Property Values

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




Return Value

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

Example

The following code shows how to check if a URL field can automatically get focus upon page load.


<!DOCTYPE html>
<html>
<body>
Homepage: <input type="url" id="myURL" autofocus>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--   ww  w.j a v  a 2 s.com-->
    var x = document.getElementById("myURL").autofocus;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows: