Javascript: Multiply values in different in two input boxes
I have a table wrapped inside a form. There are several rows each with a quantity field, price and total. I would like to give the result of the total field by using Javascript to multiply the values in the quantity field and price field.
<script type="text/javascript">
function calculate() {
var quantity = document.getElementById('qty').value;
var price = document.getElementById('prc').value;
var total = document.getElementById('ttl');
var result = quantity * price;
total.value = result;
}
</script>
<script type="text/javascript">
var table = document.getElementById("assessTable");
for (var i = 0, row; row = table.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
function calculate() {
var quantity = document.getElementById('qty').value;
var price = document.getElementById('prc').value;
var total = document.getElementById('ttl');
var result = quantity * price;
total.value = result;
}
}
}
</script>
Did you read what I said? Your elements can't have the same ID, you now give them all the same ID... You need to select the element with a certain class or anything else, but not IDs.
function calculate(key) {
var perUnitRate = document.getElementById('perUnitRate'+key).value;
var totalUnite = document.getElementById('totalUnite'+key).value;
var result = document.getElementById('result'+key);
var myResult = perUnitRate * totalUnite;
result.value = myResult;
}