?
Solved

Javascript Help pls!

Posted on 2006-05-09
10
Medium Priority
?
177 Views
Last Modified: 2008-03-10
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
Comment
Question by:lunchboxbill
  • 5
  • 5
10 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 400 total points
ID: 16640788
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
 
LVL 1

Author Comment

by:lunchboxbill
ID: 16647312
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 16647333
Do you set sun_total_2 = zero before the loop?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:lunchboxbill
ID: 16647355
yes i do
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16647398
Is you page accessible online?
0
 
LVL 1

Author Comment

by:lunchboxbill
ID: 16647525
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 16647578
can you please cipy also the html source as txt file there?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16647949
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
 
LVL 1

Author Comment

by:lunchboxbill
ID: 16665734
fixed it - it was me being a numpty!

thanks for all your help!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question