Skip to content

Instantly share code, notes, and snippets.

@trungx
Forked from anonymous/fiddle.css
Created March 12, 2018 05:01
Show Gist options
  • Save trungx/ff2bcc90bc65ad5caf91f45497757881 to your computer and use it in GitHub Desktop.
Save trungx/ff2bcc90bc65ad5caf91f45497757881 to your computer and use it in GitHub Desktop.
body {
background: black;
color: #d5d4d4;
font-family: Helvetica, Arial, sans-serif;
font-size: 12px;
margin: 0;
overflow-x: auto;
padding: 30px;
}
table {
background: #030303;
border-collapse: collapse;
border: 3px #393939 solid;
color: #d5d4d4;
margin: 1em 1em 1em 0;
}
thead {
border-collapse: collapse;
color: #d5d4d4;
}
th, td {
border: 1px #aaa solid;
padding: 0.2em;
}
<table>
<thead>
<tr>
<th>MAX ATK</th>
<th>MAX DEF</th>
<th>MAX HP111</th>
<th>Overall</th>
</tr>
</thead>
<tbody>
<tr>
<td class="combat">8170</td>
<td class="combat">6504</td>
<td class="combat">6050</td>
<td class="total-combat"></td>
</tr>
<tr>
<td class="combat">8500</td>
<td class="combat">10200</td>
<td class="combat">7650</td>
<td class="total-combat"></td>
</tr>
<tr>
<td class="combat">9185</td>
<td class="combat">7515</td>
<td class="combat">9185</td>
<td class="total-combat"></td>
</tr>
</tbody>
</table>
$(document).ready(function () {
//iterate through each row in the table
$('tr').each(function () {
//the value of sum needs to be reset for each row, so it has to be set inside the row loop
var sum = 0
//find the combat elements in the current row and sum it
$(this).find('.combat').each(function () {
var combat = $(this).text();
if (!isNaN(combat) && combat.length !== 0) {
sum += parseFloat(combat);
}
});
//set the value of currents rows sum to the total-combat element in the current row
$('.total-combat', this).html(sum);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment