Quick Tip - 07: Autofocus Fields With HTML5 Form Attributes
Have you ever wanted to automatically place the cursor in a field when the page is rendered? I know I have.
This sort of thing used to require more than just HTML to accomplish but with now HTML5 it’s a breeze. Here’s how you can autofocus fields with HTML5 form attributes.
<input autofocus="autofocus" id="username" type="text" />
Yep, it’s that easy. HTML5 form attributes have pretty good browser support and older browsers that don’t support them will simply ignore them.
It can be added in the following ways:
-
autofocus
-
autofocus=""
-
autofocus="autofocus"
All three do the exact same thing.
Browser Support
HTML5 autofocus has pretty good browser support working in all modern browsers. Not supported in ie9 and below.