Default html form focus without JavaScript

Adam Jenkin picture Adam Jenkin · Nov 12, 2010 · Viewed 97.7k times · Source

Is it possible to set the default input focus on an HTML form without using JavaScript, for example:

<html>
  <form>
    Input 1: <input type="text" name="textbox1"/>
    <br/>
    Input 2: <input type="text" name="textbox2"/>
  </form>
</html>

I want to set the default focus to either of the text-boxes when the form loads without using JavaScript (as I want the behaviour to occur when a user has js disabled).

Answer

Lucas picture Lucas · Nov 12, 2010

You can do it in HTML5, but otherwise, you must use JavaScript.

HTML5 allows you to add autofocus to your form element, eg:

<input type="text" name="myInput" autofocus />

This does work in browsers which support HTML5 (Or rather, browsers which support this particular part of HTML5) but as you know, not everybody can use it yet.