My goal is to be able to compute the subtotal from given 2 values. First value is given, and the other one is inputted. The subtotal changes when the user inputs a different value into the quantity field.
Here's the jquery code that I'm using:
<script type="text/javascript">
$(function(){
$('input[id^=qty]').keyup(function(){
var parentDiv = $(this).closest('#axd');
var curprice=parentDiv.find('input[id^=price]').val();
var curqty= this.value;
var curtotal=curprice * curqty;
parentDiv.find('input[id^=comp]').val(curtotal);
});
});
And here's the body:
<tr>
<form name="cartform" method="get" onsubmit="return validate_form(this);" action="checkout.php">
<div id="axd">
<td><?php echo $name; ?></td>
<input type="hidden" id="pids" name="ids[]" value="<?php echo $id; ?>"/>
<input type="hidden" name="qoh[]" value="<?php echo $qtyhand; ?>"/>
<input type="hidden" name="dprice[]" value="<?php echo $dsprice; ?>"/>
<td><?php echo $qtyhand; ?></td>
<td><input type="text" id="<?php echo 'qty' . $id; ?>" name="qbuys[]" value=""/></td>
<input type="hidden" id="<?php echo 'price' . $id; ?>" name="sprice[]" value="<?php echo $ssprice; ?>"/>
<td><?php echo $ssprice; ?></td>
<td><input type="text" name="subtot[]" id="<?php echo 'comp' . $id; ?>" value=""/></td>
<td><a href="viewcart.php?action=zeroline&id=<?php echo $id; ?>"><img src="../img/system/delete-icon.png"></img></a></td>
</div>
</form>
</tr>
The error is that it doesn't output any value at all. What's wrong with my code? Here's the link to the full version of the code: http://cu.pastebin.com/sWxFV6Hg
why not just assign each field an ID and use the classic javascript instead of jquery for one multiply and a sum.
go like this: - assign ids then write this function:
function operation()
{
var op1 = document.getElementById('id1').value;
var op2 = document.getElementById('id2').value;
document.getElementById('wherever_you_want_it').value = op1 (whatever operation) op2;
}
Call the function on whatever event you want. And... done it.