I have an textarea for input of values that are rows and columns (but not a table) and I want to be able to add the values of the rows individually. My logic is that I get the user to input the number of rows they have entered, split the input to make a string and then split it up by the number of rows they input. Hereis what I have. Happy for better solutions. Alternately I did some reading and thought I could convert the rows to actual <tr>s and then go through them (I would also like to be able to this for columns at a later stage). Thanks in advance:
<html>
<head>
<script type='text/javascript'>
function sum(){
var rows= document.getElementById('rows').value;
var val = document.getElementById('userInput').value;
var temp = val.split(" ");
var lines=temp.split(rows);
var total = 0;
var v;
for(var i = 0; i < temp.length; i++) {
v = parseFloat(lines[i]);
//this is what I am missing to get each row's sum separately
}
document.write(//each row total);
}
</script>
</head>
<body>
<form id="input">
<textarea id="userInput"></textarea>
Number of rows: <textarea id="rows"></textarea>
<input id="Run" type=Button value="run" onClick="sum()" />
</form>
</body>
</html>
So now I have the following (I had to add v back in) but it is returning NaN (and noting I will address the final suggestion anyway):
<script type='text/javascript'>
function sum() {
var grandTotal = 0,
rowTotals = [], // array to hold individual row totals
rowData,
rows,
val,
var v;
rawData = document.getElementById('userInput').value;
rows = rawData.split("\n");
for (var i=0; i < rows.length; i++) {
rowTotals[i] = 0;
rowData = rows[i].split(" ");
for (var j=0; j < rowData.length; j++) {
val = parseFloat(rowData[j]);
if (!isNaN(v)) rowTotals[i] += v;
}
alert("Total for row " + (i + 1) + ": " + rowTotals[i]);
grandTotal += rowTotals[i];
}
alert("Grand total: " + grandTotal);
}
</script>
\nor\ror something and then get the length of the resulting array to tell you how many rows there are? Also, presumably the user has entered a number for "Number of rows:", so I don't understand why you would use that number as the parameter passed to.split(rows).