Automatically Focusing on an input Element

You specify which input element to focus with the autofocus attribute. You can apply the autofocus attribute only to one input element.

If you try to apply the element more than once, the browser will focus on the last element.

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
      <form method="post" action="http://yourServer/form">
            <p>
                  <label for="fave">Item: <input autofocus id="fave"
                        name="fave" /></label>
            </p>
            <p>
                  <label for="name">Name: <input id="name" name="name" /></label>
            </p>
            <button>Submit Vote</button>
      </form>
</body>
</html>
  
Click to view this demo.
Home 
  HTML CSS Book 
    HTML  

form:
  1. <form> to get user input
  2. Using the base Element to Set a Destination for Form Data
  3. Configuring the HTTP method Attribute for Form element
  4. Configuring the Form Data Encoding
  5. Controlling Form Completion
  6. Specifying a Target for the Form Response
  7. Setting the Name of the Form
  8. Adding Labels to a Form
  9. Automatically Focusing on an input Element
  10. Disabling Individual input Elements
  11. Form Elements Outside the Form
Related: