I wanted a simple way to set the input focus on a web page. The use case I wanted was this:
<input ... class="focus" />
All I want to do is add the focus class to a form field. To get this to work, I whipped up a little JavaScript (using Prototype):
function setDefaultFocus()
{
var focus = $$(".focus");
if ( focus.length > 0 )
{
focus[0].focus();
}
}
Event.observe(window, 'load', setDefaultFocus);
The result: After your page finishes loading, the first element found with the focus class will be given the focus. Done!
Leave a comment