Question

I am trying to create an onload script which will take cost from each table cell (skipping the first $ symbol), add them together and present the total cost with the $ symbol. It seems to work partly because the "console.log(sum);" returns the right sum value but I get a weird NaN result in the total cell. Any ideas why? Please see below. Thank you in advance, Attila

function colSum() {
var sum = 0;
var totalCost ="";
//iterate through each input cell and add to sum variable
$('td.cost').each(function() {     
    sum += parseFloat(($(this).text().substr(1)));
console.log(sum);  
$('#sumTotal').html(sum);              
});

}

And the HTML source:

<table id="BookTable" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="33%" scope="col"><h4>Title</h4></th>
<th width="18%" scope="col"><h4>Author</h4></th>
<th width="15%" scope="col"><h4>Publisher</h4></th>
<th width="6%" scope="col"><h4>Year</h4></th>
<th width="13%" scope="col"><h4>Cover Artist</h4></th>
<th width="9%" scope="col"><h4>Genre</h4></th>
<th width="6%" scope="col"><h4>Cost</h4></th>
</tr>
<tr>
<td width="33%">And Kill Once More </td>
<td width="18%">Al Fray </td>
<td width="15%">Graphic Mystery (1st)</td>
<td width="6%">1955</td>
<td width="13%">Saul Levine </td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$7.11</td>
</tr>
<tr>
<td width="33%">The Big Sleep</td>
<td width="18%">Raymond Chandler </td>
<td width="15%">New Avon Library (1st)</td>
<td width="6%">1943 </td>
<td width="13%">Paul Stahr  </td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$43.88</td>
</tr>
<tr>
<td width="33%">The Insidious Doctor Fu Manchu</td>
<td width="18%">Sax Rohmer </td>
<td width="15%">Pyramid (1st)</td>
<td width="6%">1961 </td>
<td width="13%">Mort Engle </td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$1.69</td>
</tr>
<tr>
<td width="33%">The Window At The White Cat</td>
<td width="18%">Mary Roberts Rinehart</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1938</td>
<td width="13%">Gerald Gregg</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$12</td>
</tr>
<tr>
<td width="33%">Steve Bentley's Calypso Caper                       </td>
<td width="18%">Robert Dietrich</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1961 </td>
<td width="13%">Tom Miller </td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$7</td>
</tr>
<tr>
<td width="33%">Dead Man's Diary and a Taste for Cognac</td>
<td width="18%">Brett Halliday</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1964</td>
<td width="13%">Robert McGinnis</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$2</td>
</tr>
<tr>
<td width="33%">House of Darkness</td>
<td width="18%">Allan MacKinnon</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1947</td>
<td width="13%">Gerald Gregg</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$3</td>
</tr>
<tr>
<td width="33%">The Panic Stricken</td>
<td width="18%">Mitchell Wilson</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1948 </td>
<td width="13%">Gerald Gregg?</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$6</td>
</tr>
<tr>
<td width="33%"> Hang By Your Neck</td>
<td width="18%">Henry kane</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1949</td>
<td width="13%">Victor Kalin</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$7</td>
</tr>
<tr>
<td>Odor of Violets</td>
<td>Baynard Kendrick </td>
<td>Dell (1st)</td>
<td>1947</td>
<td>Gerald Gregg</td>
<td>Mystery</td>
<td class="cost">$2</td>
</tr>
<tr>
<td>Top of the Heap</td>
<td>A.A.Fair (Erle Stanley Gardner)</td>
<td>Dell</td>
<td>1954</td>
<td>Griffith Foxley</td>
<td>Mystery</td>
<td class="cost">$5</td>
</tr>
<tr>
<td>Gods and Goddesses In Art and Legend</td>
<td>Herman J. Wechsler</td>
<td>Pocket Book (1st)</td>
<td>1950</td>
<td>N/A (photo)</td>
<td>Art</td>
<td class="cost">$0.50</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>Total:</td>
<td id="sumTotal"></td>
</tr>
</table>
Was it helpful?

Solution

Fixed js:

var sum=0
//iterate through each input cell and add to sum variable
$('td.cost').each(function() {     
    sum += parseFloat(($(this).text().substr(1)));
    console.log(sum);  
    $('#sumTotal').html(sum);              
});

Working example: http://jsfiddle.net/4PZFL/1/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top