[thelist] dynamic javascript loop help

Joshua Olson joshua at waetech.com
Tue Jan 18 10:58:56 CST 2005


> -----Original Message-----
> From: Chris Heilmann
> Sent: Tuesday, January 18, 2005 8:35 AM
> 
> > You need quotes around your strings. Try this
> > nDatarow = "datarow_" + a + "_col_" + f;
> > nValue = "row_" + a + "_col_" + f;
> > theform.nDatarow .value = document.getElementById(nValue).innerHTML;
> 
> Have you tried that? In more browsers than IE? The question 
> was answered properly about three times already.

Chris,

Oddly enough, none of the provided answer supplied what I personally would
consider the "proper" way to do it.

Here's my version, take it or leave it, based strictly on the original code:

function initvalues() {

	var theform = document.getElementById("frmdefault");

	for(var a=0; theform.elements.ycounter.value -1; a++)
	{
		for(var f=0; theform.elements.fcount.value; f++)
		{
			theform.elements['datarow_'+a+'_col_'+f].value = 
	
document.getElementById('row_'+a+'_col_'+f).innerHTML;
		}
	}
	
}

Caveat: Untested code, and innerHTML is not standards compliant [0].
Perhaps we could use innerText instead?

As a rule of thumb, and in my experience, it's a "best practice" to
reference a form's fields through the "elements" collection of the form
object.  That will help resolve any ambiguity if form elements are named the
same as attributes of the form object... such as "name" or "action" or
"method".  Likewise, accessing the fields via the elements collection is a
handy way to avoid the eval function, as demonstrated above.

[0] - http://www.developer-x.com/content/innerhtml/

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




More information about the thelist mailing list