Javascript examples for DOM HTML Element:Button
The autofocus property sets or gets whether a button should get focus when the page loads, or not.
This property reflects the HTML autofocus attribute.
Set the autofocus property with the following Values
Value | Description |
---|---|
true|false | Sets whether a button should get focus when the page loads |
A Boolean, returns true if the button automatically gets focus when the page loads, otherwise it returns false
The following code shows how to check if a button gets focus upon page load:
<!DOCTYPE html> <html> <body> <button id="myBtn" autofocus>My Button</button> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {/*from w w w . j a v a 2 s. com*/ var x = document.getElementById("myBtn").autofocus; document.getElementById("demo").innerHTML = x; } </script> </body> </html>