Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with caculations

    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.

    Thanks,


    <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;
    }
    </script>

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kentucky
    Posts
    133
    Thanks
    0
    Thanked 1 Time in 1 Post
    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?

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Whatever element it is, it's sure its value is a string as all field values are strings.
    Code:
    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().


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •