How to you dynamically address field from html page

J

Jon Knutson

I have a variable number of lines, with each line being a transaction,
displayed in my jsp. Each line has an amount value with the name
equal to the line it is displayed on. For example name=amount0 on
line 1
name=amount1 on line 2, etc...

I am attempting to edit the values in the amount field prior to
submitting the form. I have a for loop that goes through the lines
but haven't been able to come up with a way to dynamically address the
field. In the example below I was hoping that 'i' would append itself
to the field name 'tranLimit'. Unfortunatly, it doesn't. How would I
make the field name variable?




isMoney = /^\d+\.\d{2}$/;

// See list of all accounts available to user
function submit_form() {
continueSubmit = "Y";
rowCount = document.chglimits.rowCount.value;
for(i=0;i<rowCount;i++) {
if(!isMoney.test(document.chglimits."tranLimit"i.value)) {
alert('Invalid Dollar Amount');
continueSubmit = "N";
} else {
}
}
if(continueSubmit == "Y") {
document.chglimits.cmd.value = 'chg_tranlimit';
document.forms['chglimits'].submit();
}
}
 
M

Michael Winter

I have a variable number of lines, with each line being a transaction,
displayed in my jsp. Each line has an amount value with the name
equal to the line it is displayed on. For example name=amount0 on
line 1
name=amount1 on line 2, etc...

I am attempting to edit the values in the amount field prior to
submitting the form. I have a for loop that goes through the lines
but haven't been able to come up with a way to dynamically address the
field. In the example below I was hoping that 'i' would append itself
to the field name 'tranLimit'. Unfortunatly, it doesn't. How would I
make the field name variable?

If you used the collection syntax for accessing forms and their controls,
you can build up names with the concatenation operator. Your attempt would
be written:

document.forms['chglimits'].elements['tranLimit' + i].value

Mike
 
K

kaeli

I am attempting to edit the values in the amount field prior to
submitting the form. I have a for loop that goes through the lines
but haven't been able to come up with a way to dynamically address the
field. In the example below I was hoping that 'i' would append itself
to the field name 'tranLimit'.

So close...
But close only counts with grenades and horseshoes.
for(i=0;i<rowCount;i++) {
if(!isMoney.test(document.chglimits."tranLimit"i.value))

Now come on - you can't just stick a variable name after quotes in JSPs,
either. :)

document.chglimits.elements["tranLimit"+i].value

Note: the above is not good cross-browser syntax.
If supporting non-IE browsers, use
document.forms["chglimits"].elements["tranLimit"+i].value

--
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top