www.webdeveloper.com
Results 1 to 7 of 7

Thread: [Help] Simple javascript formula

  1. #1
    Join Date
    Jun 2012
    Posts
    5

    [Help] Simple javascript formula

    Hi,

    I'm currently trying to do a formula calculation for my form, but for some reason I cannot seem to get it to work.

    basically my formula is (H x W x L) / 166

    Code:
    function calculateText(){
    
    var op1=document.getElementById('height');
    var op2=document.getElementById('width');
    var op3=document.getElementById('length');
    var result=document.getElementById('total');
    
    if(op1.value=="" || op1.value!=parseFloat(op1.value)) op1.value=0;
    
    if(op2.value=="" || op2.value!=parseFloat(op2.value)) op2.value=0;
    
    if(op3.value=="" || op3.value!=parseFloat(op3.value)) op3.value=0;
    
    result.value=0;
    
    result.value=parseInt(result.value);
    
    result.value=parseInt(result.value)+parseInt(op1.value) * (op2.value) * (op3.value) / 166);}

  2. #2
    Join Date
    Jun 2012
    Posts
    5
    What am I doing wrong?

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    Javascript tends to make concatenation rather than additions. Then it's always careful to begin with the multiplications to force Number conversions...
    Code:
    function calculateText(){
       var op1=parseFloat(document.getElementById('height'));
       0var op2=parseFloat(document.getElementById('width'));
       var op3=parseFloat(document.getElementById('length'));
       var result=op1*op2*op3/166;
       if (!result || isNaN(result)) {alert("The values seems erroneous...");return}
       result=result.toFixed(2);
       document.getElementById('resultSpan').innerHTML=result;
    }
    Last edited by 007Julien; 06-18-2012 at 06:05 AM.

  4. #4
    Join Date
    Jun 2012
    Posts
    5
    Code:
    <script>
    function calculateText(){
       var op1=parseFloat(document.getElementById('height'));
       var op2=parseFloat(document.getElementById('width'));
       var op3=parseFloat(document.getElementById('length'));
       var result=op1*op2*op3/166(document.getElementById('total'));
       if (!result || isNaN(result)) {alert("The values seems erroneous...");return}
       result=result.toFixed(2);
       document.getElementById('resultSpan').innerHTML=result;
    }
    </script>
    Doesn't seem to work. I'm using this in conjunction with a form component "RSFORM!PRO" for my website. I used the example here, but all it does is add.

    http://demo.rsjoomla.com/calculation-form-example

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    It should work with a HTML line like this to display the result :
    Code:
    <p>The result is <span id="resultSpan"></span></p>

  6. #6
    Join Date
    Jun 2012
    Posts
    5
    Nope, not working at all. I've tried it, and the html code.

    However this code works http://demo.rsjoomla.com/calculation-form-example
    but it just adds, I need it to multiply 3 fields, and divide it by 166

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    Sorry, I forgot the values, try this which makes the product...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1250">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>Calculation</title>
    <style type="text/css">
    body {background-color:#000;color:#fff;}
    #pge {display:block;width:300px;margin:20px auto;}
    input{width:60px;text-align:center}
    p{margin:0;}
    </style>
    </head>
    <body>
    <div id="pge"><fieldset><legend>Calculation</legend> 
    <p><Label><input id="width" type="text" onblur="calculateText()" value="0">&nbsp;Width - centim&#232;trer</label></p>
    <p><Label><input id="height" type="text" onblur="calculateText()" value="0">&nbsp;Height - centim&#232;trer</label></p>
    <p><Label><input id="length" type="text" onblur="calculateText()" value="0">&nbsp;Length - centim&#232;trer</label></p>
    <p>&nbsp;</p>
    <p>The volume is <span id="rsp">0</span> cubic centimetrer<span id="rsq"></span></p>
    </fieldset>
    </div>
    <script type="text/javascript">
    function calculateText(){
       var op1=document.getElementById('height').value;
       var op2=document.getElementById('width').value;
       var op3=document.getElementById('length').value;
       var result=op1*op2*op3;
       document.getElementById('rsp').innerHTML=isNaN(result)?'undefined':result;
       var s='';if (!isNaN(result) && 0<result-1) s='s';
       document.getElementById('rsq').innerHTML=s; 
    }
    window.onload=calculateText();
    </script>
    </body>
    </html>
    Good luck to divide by 166 with a debugger to see what is not working !
    Last edited by 007Julien; 06-20-2012 at 07:19 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles