ninedoors Posted May 26, 2008 Share Posted May 26, 2008 I have a function that takes an input and writes that input to an html table cell. I have 18 cells that the user inputs a number to. What I want to is add the numbers the user inputs up and write it to the final cell as a total. z, y have been declared outside the function so they are global. Here's the function: function s (t){ var holes = [ "hole21","hole22","hole23","hole24","hole25","hole26","hole27","hole28","hole29","hole31","hole32","hole33","hole34","hole35","hole36","hole37","hole38","hole39" ]; var r = holes[z]; document.getElementById(r).innerHTML=t.innerHTML; z++; y += t.innerHTML; if(r == 'hole29') { document.getElementById('hole30').innerHTML=y; } } Where t is the user input(an integer), holes is the array of table cell ids where the user input values are being shown My first thought was to just have a variable that would take would be say var y and then have something like y =y+t; And have a running total, then when I reached the given cell that I wanted to put the total in (holes30) I could just write y to that cell. Does that sound right? I tried do that but I could get it to work any suggestions? Quote Link to comment Share on other sites More sharing options...
ninedoors Posted May 26, 2008 Author Share Posted May 26, 2008 Here's the working code in case any one is interested function s (t){ var holes = [ "hole21","hole22","hole23","hole24","hole25","hole26","hole27","hole28","hole29","hole31","hole32","hole33","hole34","hole35","hole36","hole37","hole38","hole39" ]; var r = holes[z]; document.getElementById(r).innerHTML=t.innerHTML; var scores = new Array(); z++; if(r == 'hole29') { ftotal = 0; for(var i=0;i<9;i++) { var u = holes[i]; var p = document.getElementById(u).innerHTML; ftotal+=Number(p); } document.getElementById("hole30").innerHTML=ftotal; } if(r == 'hole39') { btotal = 0; total = 0; for(var i=9;i<18;i++) { var u = holes[i]; var p = document.getElementById(u).innerHTML; btotal+=Number(p); } document.getElementById("hole40").innerHTML=btotal; total = ftotal + btotal; document.getElementById("hole41").innerHTML=total; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.