By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,567 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,567 IT Pros & Developers. It's quick & easy.

Referencing form elements

P: n/a
<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?

Oct 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

Oct 15 '06 #2

P: n/a
VK

th*****@gmail.com wrote:
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;
}

Oct 15 '06 #3

P: n/a
VK wrote on 15 okt 2006 in comp.lang.javascript:
>
th*****@gmail.com wrote:
>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 ;-(

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 15 '06 #4

P: n/a
VK
Evertjan. wrote:
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>

Oct 15 '06 #5

P: n/a
th*****@gmail.com wrote:
<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/bes...#squarebracket

This was written specifically for questions like this!

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.