
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM Button autofocus Property
The HTML DOM Button autofocus property is associated with autofocus property of the <button> element. The button autofocus property is used to specify whether a button on the HTML document should get the focus or not when the page loads.
Syntax
Following is the syntax for −
Setting the button autofocus property −
buttonObject.autofocus = true|false
Here, the true|false specifies if the given input button should get the focus on not when the page loads.
- True − Input button gets focus
- False − Input button doesn’t get focus.
Example
Let us see an example for the HTML DOM button autofocus property −
<!DOCTYPE html> <html> <body> <button type="button" id="MyButton" autofocus>BUTTON</button> <p>Click the below button to know if the input button above automatically gets the focus on page load or not</p> <button onclick="buttonFocus()">CLICK IT</button> <p id="Sample"></p> <script> function buttonFocus() { var x = document.getElementById("MyButton").autofocus; if(x==true) document.getElementById("Sample").innerHTML="The input button does get focus on page load"; else document.getElementById("Sample").innerHTML="The input button does not get focus on page load"; } </script> </body> </html>
Output
This will produce the following output −
On clicking CLICK IT button −
In the above example −
We have a button with id “MyButton” and autofocus property enabled −
<button type="button" id="MyButton" autofocus>BUTTON</button>
We then have a CLICK IT button to execute buttonFocus() function −
<button onclick="buttonFocus()">CLICK IT</button>
The buttonFocus() function gets the button element using the getElementById() method and gets its autofocus value, which is Boolean and assigns it to the variable x. Using the conditional statements we check whether the autofocus value is true and false and display suitable text accordingly in a <p> element with id “Sample” associated with it.
function buttonFocus() { var x = document.getElementById("MyButton").autofocus; if(x==true) document.getElementById("Sample").innerHTML="The input button does get focus on page load"; else document.getElementById("Sample").innerHTML="The input button does not get focus on page load"; }
- Related Articles
- HTML DOM Textarea autofocus Property
- HTML DOM Select autofocus Property
- HTML DOM Input URL autofocus Property
- HTML DOM Input Week autofocus Property
- HTML DOM Input Password autofocus Property
- HTML DOM Input Search autofocus Property
- HTML DOM Input Submit autofocus property
- HTML DOM Input Text autofocus Property
- HTML DOM Input Radio autofocus property
- HTML DOM Input Range autofocus property
- HTML DOM Input Reset autofocus property
- HTML DOM Input Checkbox autofocus Property
- HTML DOM Input Month autofocus Property
- HTML DOM Input Number autofocus Property
- HTML DOM Input Color autofocus Property
