Reputation: 5
I have a trouble with each()
in JQuery. I have a table contain information about student include: name, math score, physical score, chemistry score and last column is average score has default value is "?".
I want when I click to button account average, column average score will display average = (math + physical + chemistry)/3. Currently, I leave the class of the 3 score columns as score
and iterate like this:
$("#btn-average").click(function() {
var sum = 0;
$(".score").each(function(value) {
value = parseFloat($(this).text());
sum += value;
});
var average = sum / 3;
$(".average-score").text(average.toFixed(1));
});
However, if when I add a new student to the table, all the points will be added when the average is calculated, I don't know what to do. Please, help me, thank you so much. This is my table
<table id="my-table" class="student-score">
<tr>
<th class="table-name-column"></th>
<th class="table-name-column">name</th>
<th class="table-name-column">math score</th>
<th class="table-name-column">physical score</th>
<th class="table-name-column">chemistry score</th>
<th class="table-name-column">average score</th>
</tr>
<tr class="student-info">
<td class="table-content">1</td>
<td class="table-content">Peter</td>
<td id="math-score" class="table-content score">9</td>
<td id="physical-score" class="table-content score">8.5</td>
<td id="chemistry-score" class="table-content score">7.5</td>
<td id="average-score" class="table-content average-score">?</td>
</tr>
</table>
Upvotes: 0
Views: 155
Reputation: 477
The Problem is that you itterate over all ".score" fields in one go, you need to break that up into induvidual chunks.
The most common way would be to itterate the rows, and inside that itterate the ".score" fields.
Here is an example:
$("#btn-average").click(function() {
$(".student-info").each(function() {
let sum = 0;
let count = 0;
$(this).find(".score").each(function() {
value = parseFloat($(this).text());
sum += value;
count++;
});
const average = sum / count;
$(this).find(".average-score").text(average.toFixed(1));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="my-table" class="student-score">
<tr>
<th class="table-name-column"></th>
<th class="table-name-column">name</th>
<th class="table-name-column">math score</th>
<th class="table-name-column">physical score</th>
<th class="table-name-column">chemistry score</th>
<th class="table-name-column">average score</th>
</tr>
<tr class="student-info">
<td class="table-content">1</td>
<td class="table-content">Peter</td>
<td id="math-score" class="table-content score">9</td>
<td id="physical-score" class="table-content score">8.5</td>
<td id="chemistry-score" class="table-content score">7.5</td>
<td id="average-score" class="table-content average-score">?</td>
</tr>
<tr class="student-info">
<td class="table-content">2</td>
<td class="table-content">Max</td>
<td id="math-score" class="table-content score">3</td>
<td id="physical-score" class="table-content score">5.5</td>
<td id="chemistry-score" class="table-content score">9.5</td>
<td id="average-score" class="table-content average-score">?</td>
</tr>
</table>
<button id="btn-average">calc sum</button>
Upvotes: 2
Reputation: 616
First You must use each
for row with student-info
class and calculate sum of its score
class elements values then calculate average for this row and continue this procedure for other row with student-info
class
$("#btn-average").click(function() {
$(".student-info").each(function(){
var sum = 0;
$(this).find(".score").each(function(value) {
value = parseFloat($(this).text());
sum += value;
});
var average = sum / 3;
$(this).find(".average-score").text(average.toFixed(1));
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="my-table" class="student-score">
<tr>
<th class="table-name-column"></th>
<th class="table-name-column">name</th>
<th class="table-name-column">math score</th>
<th class="table-name-column">physical score</th>
<th class="table-name-column">chemistry score</th>
<th class="table-name-column">average score</th>
</tr>
<tr class="student-info">
<td class="table-content">1</td>
<td class="table-content">Peter</td>
<td id="math-score" class="table-content score">9</td>
<td id="physical-score" class="table-content score">8.5</td>
<td id="chemistry-score" class="table-content score">7.5</td>
<td id="average-score" class="table-content average-score">?</td>
</tr>
<tr class="student-info">
<td class="table-content">1</td>
<td class="table-content">Peter</td>
<td id="math-score" class="table-content score">8</td>
<td id="physical-score" class="table-content score">9.5</td>
<td id="chemistry-score" class="table-content score">6.5</td>
<td id="average-score" class="table-content average-score">?</td>
</tr>
</table>
<input type="button" id="btn-average" value="AVG" />
Upvotes: 0