Click to See Complete Forum and Search --> : Javascript enhancement


rchoppin
05-01-2003, 07:32 PM
Could javascript be changed so that single occurances of a form element be refrenced as element[0] rather than forcing us to test for multiple occurances? I don't think this would break legacy code and only cost a little bit of client side memory.
Here is an example of the kind of code that results from the lack of this feature:
if ( document.act_form.chg.length == null ) {
document.act_form.chg.value = '*'
} else {
document.act_form.chg[0].value = '27'
}

On the otherhand perhaps there is a simple solution that i'm missing. This is particularly useful to those of use who generate input elements as a function of db queries in tools like coldfusion and php.

Nedals
05-01-2003, 08:23 PM
I'm not sure what you are asking...
but form elements can be referenced using:

document.formname.elementname
or
document.formname.elements[n] // note the 's'
or
document.forms[n].elements[n]

A single form with one element would be referenced with..
document.forms[0].elements[0]


Any help??

rchoppin
05-01-2003, 08:38 PM
That would work with a) a single form having b) a single 1-or-more repeating element, but I have very few of those to deal with. Most of my documents have multiple forms and multiple elements repeating 1 or more times.
You can use the form and element arrays but you have to compare names in order to figure out which element is which.