Click to See Complete Forum and Search --> : Element Arrays


JMH180471
07-28-2003, 07:09 AM
I have several textboxes, all names "Name1", "Name2", "Name3", etc.

What I want to do is populate those textboxes consecutivly using a loop.

The following is similar to what I'm after...
for(i=0;i<numberCount;i++)
{
Name(i).innerText=i
}

It's as simple as that!

Any help would be appreciated,

TIA

AdamBrill
07-28-2003, 07:21 AM
You were actually pretty close. ;) Try this instead:for(i=0;i<numberCount;i++){
document.formname.elements['Name'+i].value=i;
} Let me know if you have any problems with it. :)

Charles
07-28-2003, 07:26 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
<!--
onload = function () {var i; for (i=1; i<=4; i++) {document.forms[0]['Name'+i].value = i}}

// or

onload = function () {var i; for (i=1; i<=4; i++) {document.getElementById('Name'+i).value = i}}
// -->
</script>
<form action="">
<div>
<input type="text" name="Name1">
<input type="text" name="Name2">
<input type="text" name="Name3">
<input type="text" name="Name4">
</div>
</form>