Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding fields - easy way??

Status
Not open for further replies.

davejam

Technical User
Jan 6, 2004
313
GB
Hi all,

i am writng an asp page which, when the process button is clicked, i want it to check the balances for all credits and debits and check to see if they are equal.

Unfortunately i have over 137 fields, 37 of which are for credits, the rest are debits.

i can simply add the fields using a simple function

Code:
 var i = parseInt(document.newPayment.inDbt1.value) + parseInt(document.newPayment.inDbt2.value)
 var j = parseInt(document.newPayment.inCrd1.value) + parseInt(document.newPayment.inCrd2.value)

and so on so i have 2 variables i and j with totals in, compare the 2, if correct continue!!

is there an easier way to do this cos like i said, theres 137 of them, pretty long code!!

I want to do this check within the page before i submit the values to the submit page without having to reload the page.

Is there a way of simplyfying my code, or an easier way to reference the values.

Any ideas? if you want me to expand on this please let me know

thank you

daveJam

*two wrongs don't make a right..... but three lefts do!!!!*
 
You can use a loop, and build up the field element names dynamically... This works because you can use strings to reference your form elements like this:

Code:
var frm = document.forms['newPayment'].elements;
var i = parseInt(frm['inDbt1'].value, 10) + parseInt(frm['inDbt2'].value, 10);

so you could replace the field names from a loop counter, etc, e.g.

Code:
var frm = document.forms['newPayment'].elements;
var i = parseInt(frm['inDbt' + someLoop].value, 10) + parseInt(frm['inDbt' + someOtherLoop].value, 10);

Don't forget the ", 10" param for parseInt - otherwise you could get unexpected results.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thankyou for the reply,

unfortunately (for me) the fields aren't laid out as simply as i stated, they are referenced by corresponding codes and named with "in" at the start, such as in0030 is a debit but in0031 is a credit, and theres no real logic to how it works these out.

I had to bite the bullet and write a nasty long function to get either of the values, updated each one to a hidden field (so i could pull it through as a request.form, meaning i only have to run it once on a process)

then in my original function that calls the calc functions i simply did a comparison and went from there..... unfortunately for me not everything is straight forward.... but its finished now.

Thanks for the suggestion, i'm sure an info nugget like that will come in very usefull with the next system i'm writing!!! (infact when i'm planning it i will definately do so with this in mind!!)

Cheers



daveJam

*two wrongs don't make a right..... but three lefts do!!!!*
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top