need help with calculation

Discussion in 'JavaScript' started by cgo85, Oct 21, 2008.

  1. #1
    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; 
    }
    Code (markup):
    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');" />
    HTML:
    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!
     
    cgo85, Oct 21, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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;
    Code (markup):
    should be:
    var a = parseInt(document.getElementById(gt_large_gage).value);
    Code (markup):
    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..
     
    xlcho, Oct 21, 2008 IP