Wp themes - Find services - Adult ADHD - Debt Consolidation - Debt Consolidation

PDA

View Full Version : need help with calculation


cgo85
Oct 21st 2008, 11:12 am
I have a form and i need to know how to multiply some values but just can't seem to figure part of it out. Here is code:

header jscript:
function a1(gt_large_gage, gt_large_color, gt_large_price) {
var a = document.getElementById(gt_large_gage).value;
var b = document.getElementById(gt_large_color).value;
var c = a * b;
var object = document.getElementById(gt_large_price);
object.value = c;
}
function a2(gt_large_price, gt_large_quantity, gt_large_total) {
var x = document.getElementById(gt_large_price).value;
var y = document.getElementById(gt_large_quantity).value;
var z = x * y;
var obj = document.getElementById(gt_large_total);
obj.value = z;
}

html form code:

<select name="gt_large_gage" onChange="javascript:a1('gt_large_gage','gt_large_color','gt_large_price');">
<option>Select Gage</option>
<option value="7.52">24 gage</option>
<option value="7.66">26 gage</option>
</select>
</div></td>
<td class="bottomborder" width="20%">
<div align="center">
<select name="gt_large_color" id="gt_large_color" onChange="javascript:a1('gt_large_gage','gt_large_color','gt_large_price');">
<option>Select Color</option>
<option value="15">Brown</option>
</select>
</div></td>
<td class="bottomborder" width="20%">
<div align="center">
<input name="gt_large_price" type="text" id="gt_large_price" size="15" onChange="javascript:a1('gt_large_gage','gt_large_color','gt_large_price');"/>
</div></td>
<td class="bottomborder" width="20%">
<div align="center">
<input name="gt_large_quantity" type="text" id="gt_large_quantity" size="15" onBlur="javascript:a2('gt_large_price','gt_large_quantity','gt_large_total');"/>
</div></td>
<td class="totals" width="20%">
<div align="center">
<input name="gt_large_total" type="text" id="gt_large_total" size="15" onBlur="javascript:a2('gt_large_price','gt_large_quantity','gt_large_total');" />

I need the value of 'gt_large_gage' * 'gt_large_color' = 'gt_large_price'

and then

This part is working correctly: value of 'gt_large_price' * 'gt_large_quantity' = 'gt_large_total'

Any help is great appreciated!

xlcho
Oct 21st 2008, 11:27 pm
It is possible that JS parses your variables as strings, which results in miscalculation.. Use parseInt() to get every value that you need from the form. This means that this code:
var a = document.getElementById(gt_large_gage).value;
should be:
var a = parseInt(document.getElementById(gt_large_gage).value);

Add some code that checks if the user has entered a number, cause if you type symbols inside the text fields, the calculation will be wrong again..