Bestimmte Zellen zusammenrechnen - einfache Addition

mgraf

Erfahrenes Mitglied
Hallo,
wahrscheinlich steh ich zu sehr auf der Leitung, aber ich hab leichte Probleme, eine einfache Addition durchzuführen.
Es sollen alle Werte, die in einer Tabelle mit der class="addition" haben zusammengezählt werden:

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Unbenanntes Dokument</title>
<style type="text/css">
.addition {}
#sumall {}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

</head>

<body>
<table width="600" border="1" id="costs">
  <tr>
    <td>Text</td>
    <td>Zahlen</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td class="addition">1000.00</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td class="addition">2000.00</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>10</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>20</td>
  </tr>
  <tr>
    <td>Summe</td>
    <td id="sumall">SUMALL</td>
  </tr>
</table>
<script type="text/javascript" >
var TotalPrice = 0.0;
$('#costs').each(function(){
       TotalPrice = TotalPrice + parseFloat($(this).find('td.addition').text());
	   $("#sumall").text(TotalPrice);
});

</script>
</body>
</html>
 
So sollte es eigentlich gehen
Javascript:
var TotalPrice = 0.0;
$('#costs td.addition').each(function(){
       TotalPrice += parseFloat($(this).text());
});
$("#sumall").text(TotalPrice);
 
Zuletzt bearbeitet von einem Moderator:
Zurück