Referencing form elements

T

theseer

<input type='text' name='foo1bar' />
<input type='text' name='foo2bar' />

If I wanted to set a value for both of these in a for loop how would I
go about referencing these elements with a changing value... that is:

What would be functionally equivalent but actually working of this:

for(i = 1;i <= 2;i++) {
document.formfromabove.foo+i+bar.value;
}

The 'foo+i+bar' part obviously doesn't work. What would I have to do to
actually achieve such a thing?
 
T

theseer

If the above request isn't possible.

How can I append HTML code to an element without resetting it.

If you innerHTML .= foobar or innerhtml = innerhtml + foobar and you
have forms in the element you are changing the forms are reset.
Ultimately I'm trying to either replace the values after the new code
is added or find a way to append to the end without resetting the form
values.
 
V

VK

What would be functionally equivalent but actually working of this:

for(i = 1;i <= 2;i++) {
document.formfromabove.foo+i+bar.value;
}

for(var i = 1; i <= 2; i++) {
document.forms['formName'].elements['foo'+i+'bar'].value = i;
}
 
E

Evertjan.

VK wrote on 15 okt 2006 in comp.lang.javascript:
What would be functionally equivalent but actually working of this:

for(i = 1;i <= 2;i++) {
document.formfromabove.foo+i+bar.value;
}

for(var i = 1; i <= 2; i++) {
document.forms['formName'].elements['foo'+i+'bar'].value = i;
}

===

with document.forms['formName'] {
.elements['foo1bar'].value = 1;
.elements['foo2bar'].value = 2;
};

===

with document.forms['formName'] {
i=1;while (i<=2)
.elements['foo'+i+'bar'].value = i++;
}

===

var f = document.forms['formName'].elements;
f['foo1bar'].value = 1;
f['foo2bar'].value = 2;

not tested ;-(
 
V

VK

Evertjan. said:
with document.forms['formName'] {
.elements['foo1bar'].value = 1;

JavaScript doesn't have (unfortunately, IMHO) default object accessor
shortcut, so this code will lead to a syntax error.

<kidding>
Hbu... Nuu... Funzr... !@#$%... Lbh qba'g xabj wninfpevcg!... Bu!...
(Ohg jr funyy fxvc ba guvf hfryrff Unyybjrra fbhaqgenpx :)
</kidding>
 
M

Matt Kruse

<input type='text' name='foo1bar' />
<input type='text' name='foo2bar' />
If I wanted to set a value for both of these in a for loop how would I
go about referencing these elements with a changing value... that is:
for(i = 1;i <= 2;i++) {
document.formfromabove.foo+i+bar.value;
}
The 'foo+i+bar' part obviously doesn't work. What would I have to do
to actually achieve such a thing?

http://www.javascripttoolbox.com/bestpractices/#squarebracket

This was written specifically for questions like this!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top