<html>
<script>
function myFunction() {
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
var c = document.getElementById("c").value;
var d = document.getElementById("d").value;
var erg = document.getElementById("erg").value;
var sum = a + b + c + d;
document.getElementById("erg").innerHTML = sum;
</script>
<body>
<table border="1">
<tr>
<td id="a">2</td>
<td id="b">4</td>
<td id="c">5</td>
<td id="d">3</td>
<td id="erg"></td>
<td>
<button type="button" onclick="myFunction()">Sum</button>
</td>
</tr>
</table>
</body>
</html>
Asked
Active
Viewed 78 times
-3

Sterling Archer
- 22,070
- 18
- 81
- 118

LelouchYagami
- 1
- 1
-
parseint the numbers. – user3227295 Oct 26 '15 at 20:11
-
Please read the help center on how to ask a question. As of now, you have asked no question. – Sterling Archer Oct 26 '15 at 20:12
-
add close your myFunction (missing a '}') – hubson bropa Oct 26 '15 at 20:13
-
This isn't entirely a duplicate. The base problem is that td's don't have value attributes. Once those are changed, then adding strings as numbers becomes the problem. – blm Oct 26 '15 at 20:16
1 Answers
2
parse int the number, and td not has value, use innertext
<html>
<script>
function myFunction() {
var a =parseInt(document.getElementById("a").innerText);
var b = parseInt(document.getElementById("b").innerText);
var c = parseInt(document.getElementById("c").innerText);
var d = parseInt(document.getElementById("d").innerText);
var sum = a + b + c + d;
document.getElementById("erg").innerHTML = sum;
}
</script>
<body>
<table border="1">
<tr>
<td id="a">2</td>
<td id="b">4</td>
<td id="c">5</td>
<td id="d">3</td>
<td id="erg"></td>
<td>
<button type="button" onclick="myFunction()">Sum</button>
</td>
</tr>
</table>
</body>
</html>

user3227295
- 2,168
- 1
- 11
- 17
-
recommend adding the radix argument in for parseInt, http://stackoverflow.com/questions/10398834/using-javascript-parseint-and-a-radix-parameter – hubson bropa Oct 26 '15 at 20:17