[thelist] New subscriber + form + adressing

Joshua Olson joshua at waetech.com
Mon Jan 12 11:40:43 CST 2004


From: "Andreas Wahlin" <Andreas.Wahlin at ufl.gu.se>
Sent: Monday, January 12, 2004 12:12 PM


> How to adress input tags in a form (and the form tag)?

The best way (atm) to address form elements is by using the form's name
attribute and the element's name attribute.

Thus:

document.forms['myForm'].elements['myInput']
or
document.forms.myForm.elements.myInput

At this time the id attribute is best used when needing to create labels for
elements.

Example:

<label for="myInput_id">Foo:</label>
<input type="text" name="myInput" id="myInput_id" size="22" value=""
tabindex="1" />

One last thing....

Keep in mind that all form elements include a reference back to the parent
form.  So, you can do things such as this:

<input type="text" name="myInput" id="myInput_id" size="22" value=""
tabindex="1" onchange="doSomething(this,
this.form.elements.someOtherElement);" />

function doSomething(src, dest)
{
  dest.value = src.value;
}

<tip type="Forms" author="Joshua Olson">
It's always a good idea to reference form elements through the elements
collection as opposed to directly off the form object.  This will keep you
from colliding with attributes and methods of the form object when trying to
use the reference.

Example:

document.form.myForm.elements.myInput.value

as opposed to:

document.form.myForm.myInput.value

If you stick to using the elements syntax, then you can name elements as
"name", "submit", etc, knowing full well that the code will still work
properly.
</tip>

<><><><><><><><><><>
Joshua Olson
Web Application Engineer
WAE Tech Inc.
http://www.waetech.com/service_areas/GA/
706.210.0168



More information about the thelist mailing list