[thelist] js problem

.jeff jeff at members.evolt.org
Fri Sep 13 15:53:09 CDT 2002


tom,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Tom Dell'Aringa
>
> var focusField = document.getElementById("lastname");
> (i've tried it with or without the var)
>
> [snip]
>
> function defaultFocus()
> {
> focusField.focus();
> }
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

why not ditch the global variable and call your defaultFocus() function, passing the field you want to focus?

function defaultFocus(oElement)
{
  oElement.focus();
}

and call it like this:

defaultFocus(document.forms[0].elements['lastname']);

fwiw, i noticed an issue that you may wish to address in your coding style -- the reliance on the getElementById() method.  there's really no reason to address form elements by id.  you're unnecessarily breaking form functionality (and introducing errors as far as i can tell) for older browsers.  use the elements array and the name value.  it's always worked for script enabled browsers and probably always will (i don't ever see the spec changing to deprecate the name attribute of form fields).

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/




More information about the thelist mailing list