Automatically Focusing on an input Element

Description

You can select which input element the browser focuses on when the form is displayed.

Example

The following code sets auto-focus input element with the autofocus attribute.


<!DOCTYPE HTML>
<html>
<body>
  <form method="post" action="http://example.com/form">
    <p>
      <label for="fave">Fruit: <input autofocus id="fave"
        name="fave" /></label>
    </p><!--from w  w w .  ja v  a2  s .c  o m-->
    <p>
      <label for="name">Name: <input id="name" name="name" /></label>
    </p>
    <button>Submit Vote</button>
  </form>
</body>
</html>

Click to view the demo

You can apply the autofocus attribute only to one input element. If you try to apply the element more than once, the last one gets the focus.





















Home »
  HTML CSS »
    HTML »




HTML Introduction
HTML Document
HTML Section
HTML Group Content
HTML Text Marker
HTML Table
HTML Form
HTML Embed