HTML - attributes - autofocus

revision:


Content

"autofocus" attribute : boolean attribute that puts the focus on an element syntax some examples


"autofocus" attribute : boolean attribute that puts the focus on an element

top

It is used to specify that the element should get focus when the page loads.
It supports only the following elements: <button>, <input>, <select> and <textarea>.
Only one form-associated element in a document can have this attribute specified. If there are several, the first element with the attribute set inserted, usually the first such element on the page, get the initial focus.


syntax

top

<elementName autofocus>


You can't apply autofocus on inputs of type "hidden", because hidden inputs can't be focused. Also, autofocus can only be on one element on one page. So don't use it on multiple elements.


some examples

top


codes:
                    <form style="margin-left:5vw;">
                        <label for="fname">First name:</label>
                        <input type="text" id="fname" name="fname" autofocus><br>
                        <label for="lname">Last name:</label>
                        <input type="text" id="lname" name="lname"><br>
                        <input type="submit">   
                    </form>
                

The button autofocus attribute

HTML codes
                    <p style="margin-left:5vw;">The button autofocus attribute</p>
                    <button style="margin-left:5vw;" type="button" autofocus onclick="alert('Hello world!')">click me!</button>