<style>
thead th,
tfoot td {
background-color: hsl(0 0% 0% /.8);
padding: .25rem;
color: wheat;
}
tbody tr {
cursor: default;
transition: all 250ms;
}
tbody tr:nth-child(odd) {
background-color: hsl(0 0% 0% /.2);
}
tbody tr:hover {
background-color: hsl(0 0% 0% /.4);
color: white;
}
td {
width: 6rem;
text-align: center;
padding: .15rem;
}
tfoot {
font-weight: bold;
}
tfoot td:first-child {
text-align: right;
}
.price {
font-size: 95%;
}
tbody td:last-child {
color: red;
}
</style>
<div id="container">
<h1>Chapter 12</h1>
<table id="t-1" class="sortable">
<thead>
<tr>
<th></th>
<th class="sort-alpha">Title</th>
<th class="sort-alpha">Author(s)</th>
<th class="sort-date">Publish Date</th>
<th class="sort-numeric">Price</th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="images/2862_OS.jpg" alt="Drupal 7"></td>
<td>Drupal 7</td>
<td>David <span class="sort-key">Mercer</span></td>
<td>September 2010</td>
<td>$44.99</td>
</tr>
<tr>
<td>
<img src="images/3685EN_Amazon%20SimpleDB_LITE_0.jpg" alt="Amazon SimpleDB: LITE">
</td>
<td>Amazon SimpleDB: LITE</td>
<td>Prabhakar <span class="sort-key">Chaganti</span>, Rich Helms</td>
<td>May 2011</td>
<td>$9.99</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4">Total Price</td>
<td id="totalPrice">0.00</td>
</tr>
</tfoot>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
calculateTotalPrice();
});
function calculateTotalPrice() {
const totalPrice = [...$('#t-1 tbody td:last-child')].reduce((s, e) => {
return s + parseFloat($(e).text().slice(1));
}, 0);
$('#t-1 #totalPrice').text('$' + totalPrice.toFixed(2));
}
</script>