• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Javascript Help pls!

Firstly, apologies this Q is only worth 95 points, its all i have!

I have a db recordset, that pulls out 7 text fields for every record (one for each day of the week)

at the moment there are only 2 rows, so its not too difficult, but inthe future there will be anything from 2 to 100, so i need to make the page as dynamic as possible.

record 1 has the text fields named sun_1, mon_1, tue_1 etc
record 2 has the text fields name sun_2, mon_2, tue_2 etc

etc etc etc

I am adding all the texfields together to get a total for the week - adding sunday to saturday isnt a problem, as i pass all 7 values thro in the function.

the problem is when I try to add sun_1 and sun_2 etc together.

i cant really pass, a possible 700 varaibles into the javascrip function.

if i was using asp i'd use a counter and do something like

do while less than the number of rows in the record set
sun__total = sun_total + "sun_"&'
loop

is there a way i can do this in javascript??

many thanks in advance



0
lunchboxbill
Asked:
lunchboxbill
  • 5
  • 5
1 Solution
 
ZvonkoSystems architectCommented:
Yes, there is a way:

  var theForm = document.forms[0];
  var sun__total = 0;
  for(var i=1;theForm["sun_"+i];i++){
     sun__total += theForm["sun_"+i].value * 1;
  }
  alert(sun__total);




0
 
lunchboxbillAuthor Commented:
thats brilliant - working great thanks!!

the only thing is, when i change the values in one of the text fields, it doesnt update.

its called onBlur, so should recalculate the total ever time the functions called right??

I've modifed the fucntion a little

            for(var i=1;theForm["sun_"+i];i++)
            {
                  sun_tot = parseFloat(theForm["sun_"+i].value.replace(/£/, ''));
                  sun_total_2 += (sun_tot * 1);
            }
            sun_total_2 = sun_total_2.toFixed(2);
            sun_name.value = "£" + sun_total_2;

which works the first time its called, but not after that.

any ideas greatly appreciated!!!

thanks
0
 
ZvonkoSystems architectCommented:
Do you set sun_total_2 = zero before the loop?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
lunchboxbillAuthor Commented:
yes i do
0
 
ZvonkoSystems architectCommented:
Is you page accessible online?
0
 
lunchboxbillAuthor Commented:
no - its on an intranet of one of our clients

the function in its entirety can be downloaded from here

http://www.fantasticmedia.co.uk/downloads/jsfunction.txt

thanks
0
 
ZvonkoSystems architectCommented:
can you please cipy also the html source as txt file there?
0
 
lunchboxbillAuthor Commented:
0
 
ZvonkoSystems architectCommented:
Your function summarizes the hidden fields sun_1 and sun_2. Does that make sense to you?
Your editable input fields do not have a naming schema visible to me. What would be schema for row three, for example?
0
 
lunchboxbillAuthor Commented:
fixed it - it was me being a numpty!

thanks for all your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now