-1
I’m setting up a ordering system and within it, I’m showing the products I have registered. So far no problems. Within this list, I need to dynamically calculate the quantity input with the value input and display the result on the side. I have three inputs:
When I add my Avascript, in the application, only the first product that is in the list, does the dynamic calculation. My table, with the list of products loaded, is inside a While.
I don’t know if there’s anything there but I think I have to change: Document.getElementById for Class and in my inputs switch from ID to Class. However I don’t know how to do this...
var first = document.getElementById('first');
var second = document.getElementById('second');
var result = document.getElementById('result');
first.addEventListener("input", sum);
second.addEventListener("input", sum);
function sum() {
var one = parseFloat(first.value) || 0;
var two = parseFloat(second.value) || 0;
var add = one * two;
result.innerHTML = add;
console.log(result);
}
<?php
while ($rows = mysqli_fetch_assoc($resultado_produtos)) { ?>
<td class="text-center">
<input type="number" style="width: 80px;" name="quantidade[]" class="form-control" id="first">
</td>
<td class="text-center">
<input name="valor[]" value="<?php echo $rows['precountario_produto']; ?>" type="number" min="0.00" max="10000.00" step="0.01" class="form-control" id="second" placeholder="0,00" style="width: 90px;">
</td>
<td class="text-center">
<small>R$</small>
<h6 id="result"></h6>
</td>
<?php } ?>
You should not repeat the attribute
id
it is the unique identifier of the element in the document, see here.– Augusto Vasques
Hello my friend, I will see yes but, I did not understand very well...
– Ragner Moura