2
I need to display the total result by summing the values that contain the value column:
var linha=document.getElementsByClassName("calcular");
var resultado=document.getElementById('total').innerHTML = 0;
for (var i=0; i < linha.length; i++) {
resultado += parseInt(linha[i].innerHTML);
}
document.getElementById("total").innerHTML = resultado;
table {
margin: 5px;
float: left;
width: 564px;
padding: 6px;
font-weight: bold;
font-size: 10pt;
color: white;
text-align: center;
}
th {
background-color: slateblue;
color: white;
}
tr td {
background-color: white;
color: black;
text-align: center;
}
<table>
<tr>
<th>ID</th>
<th>NOME</th>
<th>VALOR</th>
</tr>
<tr>
<td>01</td>
<td>Beltrano</td>
<td class="calcular">10.50</td>
</tr>
<tr>
<td>02</td>
<td>Fulano</td>
<td class="calcular">05.98</td>
</tr>
<tr>
<td>03</td>
<td>Ciclano</td>
<td class="calcular">25.00</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> TOTAL </td>
<td> </td>
<td id="total"> </td>
</tr>
</table>
The script works in part, but I couldn’t get it to give me the full result with the decimal places(after the point).
As an answer, he returns to me: 40
When you should return to me: 41.48
At least that’s how I imagine it must be.
If you pull this in a matrix array
, to interact between the indexes can be solved or I’m wrong?
Or it solves with just a few more arithmetic expression
not just change the
parseInt
forparseFloat
?– Marcelo Shiniti Uchimura
better than using the
parseInt/Float
is to use+
:resultado += +linha[i].innerHtml;
– Marcelo Shiniti Uchimura
@Marcelouchimura Your comment was made juz to the problem that has earned me the most as a TIP! I thank you since, ja.
– Diego Henrique