View Full Version : Problem with caculations

06-30-2005, 10:01 PM
The following script on here is causing me problems.

I have a check box which calculates values based on criteria which I won't go into.

However I need it to calculate two fields and it isn't working on the second field. I originally had it working but not the way I needed, it was parsing the integers as strings and concatenating them instead of adding them. I tried parseInt() but only got errors here.

If someone can take a look and let me know what I can do to make it actually calculate that would be great.

The red Text is the one that isn't working properly.


<script type="text/javascript">
function zeroPack() {
if (document.getElementById('nopack').checked == true) {
document.getElementById('pack').value = 0;
} else {
document.getElementById('pack').value = Math.ceil(document.getElementById('invacv').value * .06);
document.getElementById('kkcode').value = document.getElementById('invacv').value + document.getElementById('pdi').value + document.getElementById('pack').value;

07-01-2005, 12:40 AM
It would be helpful to know what those elements are. Can you elaborate as to what type of elements you are referring to in each of the getElementByID calls?

07-01-2005, 04:57 AM
Whatever element it is, it's sure its value is a string as all field values are strings.

function zeroPack() {
var pack = document.getElementById('pack');
var invacv = document.getElementById('invacv');
if (document.getElementById('nopack').checked) {
pack.value = 0;
} else {
pack.value = Math.ceil(invacv.value * .06);
document.getElementById('kkcode').value = parseInt(invacv.value) + parseInt(document.getElementById('pdi').value) + parseInt(pack.value);
If the values can contain decimal numbers, then you need to use parseFloat() or Number() function instead of parseInt().