¿De qué manera puedo multiplicar y a la vez sumar esos totales, donde mi input
tiene como valor value="2.500.000,00"
?.
Quiero multiplicar y sumar sin tener que quitar los puntos y decimales, ya que si se los quito mi código funciona perfectamente, pero quiero hacerlos con los puntos y decimales.
Estoy tomando el valor de los input
por medio de los tr
.
Anexo Fragmento de mi código
<!DOCTYPE html>
<html>
<body>
<table>
<thead>
<tr>
<th>Precio</th>
<th>Cantidad</th>
<th>Precio Total</th>
</tr>
</thead>
<tbody>
<tr>
<td> <input type="text" class="input" value="2.500.000,00"> </td>
<td> <input type="number" class="input"> </td>
<td> <input type="text" class="total" disabled> </td>
</tr>
<tr>
<td> <input type="text" class="input" value="4.500.000,00" > </td>
<td> <input type="number" class="input"> </td>
<td> <input type="text" class="total" disabled> </td>
</tr>
<tr>
<td>TOTAL</td>
<td> <input type="text" class="totales" value="0" disabled> </td>
</tr>
</tbody>
</table>
</body>
</html>
<script>
var input=document.querySelectorAll(".input");
input.forEach(function(e) {
e.addEventListener("click",multiplica);
e.addEventListener("keyup",multiplica);
});
function multiplica() {
var tr=this.closest("tr");
var total=1;
var inputs=tr.querySelectorAll(".input");
inputs.forEach(function(e) {
total*=e.value;
});
tr.querySelector(".total").value=total.toFixed(2);
calcularTotal(this.closest("table"));
}
function calcularTotal(e) {
var total=0;
var totales=e.querySelectorAll(".total");
totales.forEach(function(e) {
total+=parseFloat(e.value);
});
e.getElementsByClassName("totales")[0].value=total.toFixed(2);
}
</script>