[Javascript] required entry field

David T. Lovering dlovering at gazos.com
Fri Mar 28 18:21:09 CST 2003


> All items inside the form object become slaves or somekind of captives by
> form individual, their names or IDs will not be processed, and will have no
> rights to speak in their names, only the form master will act as
> representative and is allowed to communicate with them.
> Because of that, you will have to ask the Form waht is the name or the value
> of the item(x).

Damn straight!  This is a GOOD thing!!!

> function isValid() {
> if(myForm.NeverBlank.value=='')
>    {
> myForm.item(0).focus() //if first input in the collection
>     document.forms(0).item(0).focus()//if first form in the document (uggly
> and very unoptimized)
>    }
> }

Nope.  Sorry -- but arrays are indexed by square brackets "[ ]", not parentheses "( )".  This will die
gloriously.  Also, some browsers get ornery when you start playing fast and loose with the document prefix, particularly if you done something funky with your default window name.

Where did this item(0) [sic!] jazz come from?  Perhaps you mean 'element[0]' for the first object inside the form.
This is dangerous if you put something (like a radio button or whatever) ahead of the text input. Try the syntax I outlined in my most recent message before this one.

> So these are the ways one can access inputs (items) inside the form:
> 
> a. doument.forms(n).item(n) //'n' stands for numeric value
> b. myForm.item(n)
> c. myForm.NeverBlank

Zero for zero.  Try

  document.forms[m].element[n]
  document.myForm.element[n]
  document.myForm.NeverBlank

If 'n' and 'm' are actually variables, you'll have to do it like this (on most browsers):

  var myObject = eval('document.forms[' + m + '].element[' + n + ']');
  var myObject = eval('document.myForm.element[' + n + ']');

> 
> But the best is the last.(it executes faster) 3x, if only one form in the
> document and only one item in the form, and as the number increases the
> difference of speed follows by multiplying the starting difference of speed
> compared with [a] choice. And it's more scripter-firendly.

Not surprising.  The fewer substitutions that the JavaScript engine has to make,
the faster the whole thing will run.

-- Dave Lovering


More information about the Javascript mailing list