Here's a revised sum function:
function sum(){
var form=document.getElementById('eval_edit');
if(!form) return;
var s1 = 0;
var s2 = 0;
var tbl=document.getElementById('sheet');
var iteration=tbl.rows.length-1;
for(var i=1; i<=iteration; i++){
var el = form['txtRow'+i];
if(!el) continue;
var txt = el.value;
if(txt != ( '' + Number(txt) )) continue;//reject non-numeric entries
var el2 = form['selRow'+i];
var el3 = document.getElementById('txtRowe'+i);
if(!el2 || !el3) alert('Error in calculating totals');
var percent = Number(el2[el2.selectedIndex].value)/100;
var tax = Number(txt) * percent;
el3.innerHTML = tax.toFixed(2);
s1 += Number(txt);
s2 += tax;
}
if(form['total']){ form['total'].value = s1.toFixed(2); }
if(form['taxtotal']){ form['taxtotal'].value = s2.toFixed(2); }
}
This does everything necessary to calculate the tax for each row and to calculate the two sum totals (income and tax).
You will also need some changes to the add function. Rather than do the whole thing for you, here are some clues:Because tax is calculated not user-entered, you do not need input fields for tax. Instead, the revised sum() function puts the tax for each row directly into the tax cells. Make sure the tax cell is given id = 'txtRowe'+iteration .
Ensure that el and sel are given a name and id (with identical values).
In the select menu change the options values to : value="10" value="20" value="30"
Get rid of "sum RowX" buttons (and their table cells). They are not necessary because the revised sum() function performs these calculations every time it is called.
Add the following events:el.onblur=sum;
sel.onchange=sum;
This ensures that sum() is called each time the user makes a change, without the need to click the "Sum" button. For safety, it's best to have a "Sum" button too.
Remove your hard-coded HTML row and call addrow(); in the anonymous onload function. This will guarantee that your first row is of identical format to other rows (easier to maintain).
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Azegurb,
Changes to the HTML are very simple. Changes to the addRow() function are a bit harder.
I must go out now - important birthday party (my neighbour's 70th) and may not get back her until Monday evening (GMT).
This should give you plenty of time to work through my instructions and fix the code for yourself. You may need to insert some debug statements to work out where it's going wrong. I'm sure you can do it on your own and I will check back here on Monday to see how you got on.Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Azegurb,
Neighbour's party was good, thank you.
Here is some code, but you must be sure to go through and see how it works. Learn from it, don't just accept it. I am sure you will.
<body>
<form name="eval_edit" method="POST">
<table align="center" width="75%">
<tr>
<td align="center">Balance sheet</td></tr>
<tr>
<td align="center">
<table id="sheet" border="1">
<tr><td>object</td><td>Income</td><td>Tax from income</td><td>instruktor</td></tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="Add" onclick="addrow()" />
<input type="button" value="Remove" onclick="removeRow()" />
<input type="button" value="SUM" onClick="sum()"/>
<input type="submit" value="Submit" />
</td>
</tr>
<tr>
<td align="left">
<input id="total" name="total" type="text"/> INCOME SUM
<input id="taxtotal" name="taxtotal" type="text"/> Tax SUM with desirable percent for ex: 20%
</td>
</tr>
</table>
</form>
</body>
function addrow(){
var tbl = document.getElementById('sheet');
var iteration = tbl.rows.length;
var row=tbl.insertRow(iteration);
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'txtRow'+iteration;
el.id = 'txtRow'+iteration;
el.size = 40;
el.value = '0';
el.onblur = sum;
cellRight.appendChild(el);
var cellRight1 = row.insertCell(2);
cellRight1.id = 'txtRowe'+iteration;
var cellRightsel = row.insertCell(3);
var sel = document.createElement('select');
sel.name = 'selRow' + iteration;
sel.id = 'selRow' + iteration;
sel.onchange = sum;
sel.options[0] = new Option('10%', '10');
sel.options[1] = new Option('20%', '20');
sel.options[2] = new Option('30%', '30');
cellRightsel.appendChild(sel);
sum();
}
function sum(){
var s1 = 0;
var s2 = 0;
var tbl=document.getElementById('sheet');
var iteration=tbl.rows.length-1;
for(var i=1; i<=iteration; i++){//loop through table rows
var el1 = document.getElementById('txtRow'+i);//Row's Income field
var el2 = document.getElementById('selRow'+i);//Row's percentage menu
var el3 = document.getElementById('txtRowe'+i);//Row's Tax cell
if(!el1 || !el2 || !el3) continue;
var txt = el1.value;
if(txt != ( '' + Number(txt) )) continue;//reject non-numeric entries
var tax = Number(txt) * Number(el2[el2.selectedIndex].value)/100;
el3.innerHTML = tax.toFixed(2);
s1 += Number(txt);
s2 += tax;
}
var t1 = document.getElementById('total');
var t2 = document.getElementById('taxtotal');
if(t1){ t1.value = s1.toFixed(2); }
if(t2){ t2.value = s2.toFixed(2); }
}
onload = function(){
addrow();
}
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
So you've been cross-posting your problem on another forum and you want me to explain some other guy's code when you can't get my perfectly good code working?!?!?!
Sorry.
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Azegurb,
Sorry I was blunt with you this morning. I'm sure you're a good chap but please remember, you will upset people by cross-posting other people's work on different forums and by starting multiple threads on a single topic.
I'm not so sure I am helping you by posting a whole solution - you would learn more by working at it - but here goes:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title>dinamik sheet</title>
<script>
function addrow(){
var tbl = document.getElementById('sheet');
var iteration = tbl.rows.length;
var row=tbl.insertRow(iteration);
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'txtRow'+iteration;
el.id = 'txtRow'+iteration;
el.size = 40;
el.value = '0';
el.onblur = sum;
cellRight.appendChild(el);
var cellRight1 = row.insertCell(2);
cellRight1.id = 'txtRowe'+iteration;
var cellRightsel = row.insertCell(3);
var sel = document.createElement('select');
sel.name = 'selRow' + iteration;
sel.id = 'selRow' + iteration;
sel.onchange = sum;
sel.options[0] = new Option('10%', '10');
sel.options[1] = new Option('20%', '20');
sel.options[2] = new Option('30%', '30');
cellRightsel.appendChild(sel);
sum();
}
function sum(){
var s1 = 0;
var s2 = 0;
var tbl=document.getElementById('sheet');
var iteration=tbl.rows.length-1;
for(var i=1; i<=iteration; i++){//loop through table rows
var el1 = document.getElementById('txtRow'+i);//Row's Income field
var el2 = document.getElementById('selRow'+i);//Row's percentage menu
var el3 = document.getElementById('txtRowe'+i);//Row's Tax cell
if(!el1 || !el2 || !el3) continue;
var txt = el1.value;
if(txt != ( '' + Number(txt) )) continue;//reject non-numeric entries
var tax = Number(txt) * Number(el2[el2.selectedIndex].value)/100;
el3.innerHTML = tax.toFixed(2);
s1 += Number(txt);
s2 += tax;
}
var t1 = document.getElementById('total');
var t2 = document.getElementById('taxtotal');
if(t1){ t1.value = s1.toFixed(2); }
if(t2){ t2.value = s2.toFixed(2); }
}
onload = function(){
addrow();
}
</script>
</head>
<body>
<form name="eval_edit" method="POST">
<table align="center" width="75%">
<tr>
<td align="center">Balance sheet</td></tr>
<tr>
<td align="center">
<table id="sheet" border="1">
<tr><td>object</td><td>Income</td><td>Tax from income</td><td>instruktor</td></tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="Add" onclick="addrow()" />
<input type="button" value="Remove" onclick="removeRow()" />
<input type="button" value="SUM" onClick="sum()"/>
<input type="submit" value="Submit" />
</td>
</tr>
<tr>
<td align="left">
<input id="total" name="total" type="text"/> INCOME SUM
<input id="taxtotal" name="taxtotal" type="text"/> Tax SUM with desirable percent for ex: 20%
</td>
</tr>
</table>
</form>
</body>
</html>
You just need to paste in your removeRow() function - I can't find it.
Enjoy.Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
That's cool Azegurb. I look forward to seeing you in the future.
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372