Javascript Reference - HTML DOM Keygen autofocus Property








The autofocus property sets or gets whether a keygen field can auto focus when the page loads, or not.

Browser Support

autofocus Yes No Yes Yes Yes

Syntax

Return the autofocus property.

var v = keygenObject.autofocus 

Set the autofocus property.

keygenObject.autofocus=true|false 

Property Values

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




Return Value

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

Example

The following code shows how to create a <keygen> element and set autofocus to "true".


<!DOCTYPE html>
<html>
<body>
<!--from  w w w  . ja v a2 s . c  o m-->
<button onclick="myFunction()">autofocus</button>
<button onclick="myFunction2()"No autofocus</button>
<br>

<script>
function myFunction() {
    var x = document.createElement("KEYGEN");
    x.setAttribute("autofocus", "autofocus");
    document.body.appendChild(x);
}

function myFunction2() {
    var y = document.createElement("KEYGEN");
    document.body.appendChild(y);
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

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


<!DOCTYPE html>
<html>
<body>
<!--from   w  w w .  j a  va  2  s. c o  m-->
Encryption: <keygen id="myKeygen" name="security" autofocus>
<button onclick="myFunction()">test</button>

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

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

</body>
</html>

The code above is rendered as follows: