Javascript Reference - HTML DOM Textarea autofocus Property








The autofocus property sets or gets whether a textarea can auto focus when the page loads.

Browser Support

autofocus Yes 10.0 Yes Yes Yes

Syntax

Return the autofocus property.

var v = textareaObject.autofocus 

Set the autofocus property.

textareaObject.autofocus=true|false

Property Values

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




Return Value

A Boolean type value, true if the textarea can auto focus when the page loads, otherwise false.

Example

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


<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea" autofocus>
</textarea>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--from w  w w. j  ava 2s  . c  o  m-->
<script>
function myFunction() {
    var x = document.getElementById("myTextarea").autofocus;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: