Don’t call me DOM

22 November 2010

Focusing on HTML forms fields through a link

Filed under:

I inquired earlier today if there was a way to give focus to an HTML form field through a simple link.

The reason of my inquiry was that WCAG 2.0 recommends to make it possible for the user [to] easily navigate to [detected form submission errors] to fix the problem.

But in practice, with the following HTML form:

<p><label for='email' id='emaillabel'>Email:</label>
<input type='text' id='email' name='email'></p>

neither a link to #email nor to #emaillabel will give focus to the input field.

For instance, it would be useful to be able to markup a possible error message above that form as follows:

Picture of Dominique Hazael-MassieuxDominique Hazaël-Massieux (dom@w3.org) is part of the World Wide Web Consortium (W3C) Staff; his interests cover a number of Web technologies, as well as the usage of open source software in a distributed work environment.