There are two kinds of form focus which are interesting:
- Being able to focus an element specified by the ID attribute, if it exists
- Being able to focus the first “input”, “select”, or “textarea” element which is not hidden or disabled
It would also be nice to specify that these are only relevant providing:
- There are no elements with non-default values
- No other element is already focused
After messing about with the scripts a couple hours, I managed to combine them. The resulting script has only one entry function, “focus(elementID)”, which works as follows:
- If the element ID is found, this will be focused
- If the element ID is not found, nothing will be focused, effectively disabling the script
- If the “elementID” parameter is unspecified, the first relevant form element will be focused
I’ve only tested the solution in IE 6.0 and Firefox 1.5, but it doesn’t generate any exceptions in any of them.
The focus check doesn’t work in Firefox.
Just include the following in your <head> element:
focus('firstname');. Or you can just check out the example page.
Update February 23rd, 2006: There are some problems with the site, which means that an old version of the script is in use, and it doesn’t work in IE6. Hopefully Nuxit will be able to fix it soon. Check out the CVS version to see the correct contents. Sorry for the inconvenience…