javascript - Sum up cells in a dynamic table -
i have set table
using javascript. note have reduced number of rows 1 ease. after loading, user input values in cells. there 3 cells on each row. when value typed in either cell1 or cell2, cell3 should updated automatically. formula
cell1(in column: score1) + cell2(in column: score2) = cell3(column: total)
however not working. keep getting nan
in cell3. can me this?
<!doctype html> <html> <head> <title>mp</title> <style type="text/css"> th {font-weight: bold; text-align: left; font-size: 14px; border: 1px; border-style: solid; margin: 0px; border-colapse: colapse; border-spacing: 0px;} td {font-weight: normal; font-size: 14px; border: 1px; border-style: solid; margin: 0px; border-colapse: colapse;} .sn {text-align: right;} .fin {text-align: right;} </style> </head> <body> <table id="pr"> <tbody> <tr class="thd"> <th>score a</th> <th>score b</th> <th>total</th> </tr> <tr> <td><input type="text" class="fin" onkeyup="update();" /></td> <td><input type="text" class="fin" onkeyup="update();" /></td> <td input type="text" class="fin" name="totdeduction"></td> </tr> </tbody> </table> <br /> <br /> <script> function update() { var j = document.getelementbyid("pr").rows[1].cells[0].innerhtml; var k = parseint(j); var l = document.getelementbyid("pr").rows[1].cells[1].innerhtml; var m = parseint(l); var n = (k + m); if (j ="") {k = 0;} else if (l ="") {m = 0;} document.getelementbyid("pr").rows[1].cells[2].innerhtml = n; } </script> </body> </html>
try check repaired script here: http://jsfiddle.net/9p19nngr/
function update() { var j = document.getelementbyid("pr").rows[1].cells[0].getelementsbytagname("input")[0].value; var k = parseint(j); if (isnan(k)) {k=0} var l = document.getelementbyid("pr").rows[1].cells[1].getelementsbytagname("input")[0].value; var m = parseint(l); if (isnan(m)) {m=0} var n = (k + m); document.getelementbyid("pr").rows[1].cells[2].innerhtml = n; }
you assumed var j
contains number, j = document.getelementbyid("pr").rows[1].cells[0].innerhtml
assigned j
dom object of input table cell contains. instead should assign inputs value: j = document.getelementbyid("pr").rows[1].cells[0].getelementsbytagname("input")[0].value
Comments
Post a Comment