Calculate text fields and display the results in a different text field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlymr75
    New Member
    • Mar 2013
    • 5

    Calculate text fields and display the results in a different text field

    I will have three text fields; all numbers.
    1st A
    2nd 4*B
    3rd C

    I need to divide the above by 6.

    The results show display in a separate results text field
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please show us the code you're using. Also, you will need to explain your question in more detail because I'm not sure what you're trying to do.

    Comment

    • carlymr75
      New Member
      • Mar 2013
      • 5

      #3
      I need this for both HTML/JAVASCRIPT and Android Java
      Code:
      function estimate(Opt,ML,Pess, Res)
      {
      var four = 4;
      var denominator = 6;
      var ML = ML*4;
      var numerator = Opt + ML + Pess;
      var n1 = 0;
      var n2 = 0;
      var n3 = 0;
      return Res = numerator / denominator;
      if (document.getElementById('Opt').value != "")
      n1 = parseInt(document.getElementById('Opt').value);
      if (document.getElementById('ML').value != "")
      n2 = parseInt(document.getElementById('ML').value);
      if (document.getElementById('Pess').value != "")
      n3 = parseInt(document.getElementById('Pess').value);
      
      
      }
      
      
      function calc() {
      var Res = document.getElementById('Res');
      Res.value = estimate(document.getElementById('Res').value);
      
      </script>
      
      </head>
      <body>
      <p style="text-align: center; font-size: xx-large; font-style: normal; font-variant: small-caps; font-weight: bold; color: #2D7176;">Pert Calculator</p>
      <div style="position: relative; width: 955px; left: 285px; top: 0.08px; height: 87.28px;">
      <label style="font-size: xx-large; font-style: oblique; text-align: center; width: 1010px; position: relative; left: 0px; top: 0px;">This PERT application will help you estimate PERT. The formula for PERT is ((Optimistic + (4 * Most Likely) + Pessimistic) / by 6)</label>
      </div>
      
      
      <div>
      <label style="position: relative; left: 85.00px; top: 70.93px; font-size: x-large; color: #937B62;">Optimistic</label>
      <label style="position: relative; left: 105.00px; top: 71.93px; font-size: large;">Most preferred</label>
      <input id="Opt" type="text" style="position: relative; width: 276px; height: 28px; left: 401.16px; top: 79.08px;">
      </div>
      
      <div>
      <label style="position: relative; left: 85.00px; top: 120.93px; font-size: x-large; color: #937B62;">Most Likely</label>
      <label style="position: relative; left: 105.00px; top: 121.93px; font-size: large;">More than likely the case</label>
      <input id="ML" type="text" style="position: relative; width: 276px; height: 28px; left: 308.88px; top: 124.08px;">
      </div>
      
      <div>
      <label style="position: relative; left: 85.00px; top: 170.93px; font-size: x-large; color: #937B62;">Pessimistic</label>
      <label style="position: relative; left: 105.00px; top: 171.93px; font-size: large;">Worst Case</label>
      <input id="Pess" type="text" style="position: absolute; width: 276px; height: 28px; left: 640px; top: 451px;">
      </div>
      
      <div>
      <label style="position: relative; left: 85.00px; top: 226.93px; font-size: x-large; color: #937B62;">Result</label>
      <input id="Res" type="text" style="position: relative; width: 276px; height: 28px; left: 570.67px; top: 226.35px;" readonly="" >
      </div>
      
      <div style="height: 80.44px;">
      <button value="Calculate" onclick="calc()" style="position: relative; width: 165px; height: 54.27px; left: 80px; top: 273.08px; font-size: x-large;" type="button">Calculate</button>
      </div>
      
      <div style="height: 40px;">
      <button id="reset" type="reset" value="Reset" style="position: relative; width: 125px; height: 45px; left: 1010px; top: 243.08px; font-size: x-large;">Reset</button>
      </div>
      
      <div>
      <img src="/CBA%20Logo%20Pert%20Calculator.fw.png" style="position: relative; left: 4px; top: -411.92px;">
      </div>
      
      
      
      </body>
      </html>
      Last edited by Rabbit; Mar 5 '13, 09:34 PM. Reason: Please use code tags when posting code.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Need what? You haven't explained clearly what it is you want. I find your first post confusing. And your post with the code seems to have little to no relation to your first.

        Comment

        • carlymr75
          New Member
          • Mar 2013
          • 5

          #5
          I cannot get the result text field to populate correctly.
          There are four text fields; all numbers.
          The user would enter these numbers.
          Text field 1: Opt
          Text field 2: 4*ML (4 times X)
          Text field 3: Pess

          All three text fields are added together and once added, the total will be divided by 6.

          Text field 4: Is the results of the first three fields

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            For one thing, your javascript code can't even run. You didn't close your calc function. You also call your estimate function but only pass one parameter when it expects 4. And you return from the estimate function even though there's more code below it.

            Comment

            • carlymr75
              New Member
              • Mar 2013
              • 5

              #7
              I have modified the code but the results are still not reflecting the result text field:
              Code:
              function estimate(Opt,ML,Pess, Res)
              	    {
              	        var Opt = parseInt(document.getElementById('Opt').value);
              	        var ML = parseInt(document.getElementById('ML').value);
              	        var Pess = parseInt(document.getElementById('Pess').value);
              
              	        var Res = ((Opt + (4 * ML) + Pess)/6);
              	       document.getElementById("showsum").innerHTML = Res;}
              </script>
              
              </head>
              <body>
                  <p style="text-align: center; font-size: xx-large; font-style: normal; font-variant: small-caps; font-weight: bold; color: #2D7176;">Pert Calculator</p>
              	<div style="position: relative; width: 955px; left: 285px; top: 0.08px; height: 87.28px;">
              		<label style="font-size: xx-large; font-style: oblique; text-align: center; width: 1010px; position: relative; left: 0px; top: 0px;">This PERT application will help you estimate PERT. The formula for PERT is ((Optimistic + (4 * Most Likely) + Pessimistic) / by 6)</label>
              	</div>
              
              
                  <div>
              		<label style="position: relative; left: 85.00px; top: 70.93px; font-size: x-large; color: #937B62;">Optimistic</label>
                      <label style="position: relative; left: 105.00px; top: 71.93px; font-size: large;">Most preferred</label>
              		<input id="Opt" type="text" style="position: relative; width: 276px; height: 28px; left: 401.16px; top: 79.08px;">
              	</div>
              
              	<div>
                      <label style="position: relative; left: 85.00px; top: 120.93px; font-size: x-large; color: #937B62;">Most Likely</label>
                      <label style="position: relative; left: 105.00px; top: 121.93px; font-size: large;">More than likely the case</label>
                      <input id="ML" type="text" style="position: relative; width: 276px; height: 28px; left: 308.88px; top: 124.08px;">
              	</div>
              
              	<div>
                      <label style="position: relative; left: 85.00px; top: 170.93px; font-size: x-large; color: #937B62;">Pessimistic</label>
              		<label style="position: relative; left: 105.00px; top: 171.93px; font-size: large;">Worst Case</label>
              		<input id="Pess" type="text" style="position: absolute; width: 276px; height: 28px; left: 640px; top: 451px;">
                  </div>
              
              	<div>
              		<label style="position: relative; left: 85.00px; top: 226.93px; font-size: x-large; color: #937B62;">Result</label>
              		<input id="Res" type="text" style="position: relative; width: 276px; height: 28px; left: 570.67px; top: 226.35px;" readonly=""  >
              
                  </div>
              
              	<div style="height: 80.44px;">
                      <button value="Calculate"  onclick="estimate()" style="position: relative; width: 165px; height: 54.27px; left: 80px; top: 273.08px; font-size: x-large;" type="button">Calculate</button>
              
                  </div>
              
              	<div style="height: 40px;">
              		<button id="reset" type="reset" value="Reset" style="position: relative; width: 125px; height: 45px; left: 1010px; top: 243.08px; font-size: x-large;">Reset</button>
              	</div>
              
              </body>
              </html>

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Your estimate function is still expecting 4 parameters but now you're passing no parameters at all. You also have no element with an id of showsum.
                Last edited by Rabbit; Mar 7 '13, 08:57 PM.

                Comment

                • carlymr75
                  New Member
                  • Mar 2013
                  • 5

                  #9
                  Hi Rabbit, I think I have fixed that but the fields are not adding up together, they are just stacking on each other. So if I enter values: 14, 15, and 16, the result is 141516.
                  Please tell me how to add the fields and then divide
                  Code:
                  	    function estimate(Opt, ML, Pess, Res)
                  	    {
                  	        var Opt = parseInt(document.getElementById('Opt').value);
                  	        var ML = parseInt(document.getElementById('ML').value * 4);
                  	        var Pess = parseInt(document.getElementById('Pess').value);
                  	        var Res = parseInt(document.getElementById('Res').value);
                  
                  	        document.getElementById('Res').value = (document.getElementById("Opt").value + document.getElementById('ML').value + document.getElementById('Pess').value / 6);
                  </script>
                  
                  </head>
                  <body>
                      <p style="text-align: center; font-size: xx-large; font-style: normal; font-variant: small-caps; font-weight: bold; color: #2D7176;">Pert Calculator</p>
                  	<div style="position: relative; width: 955px; left: 285px; top: 0.08px; height: 87.28px;">
                  		<label style="font-size: xx-large; font-style: oblique; text-align: center; width: 1010px; position: relative; left: 0px; top: 0px;">This PERT application will help you estimate PERT. The formula for PERT is ((Optimistic + (4 * Most Likely) + Pessimistic) / by 6)</label>
                  	</div>
                  
                  
                      <div>
                  		<label style="position: relative; left: 85.00px; top: 70.93px; font-size: x-large; color: #937B62;">Optimistic</label>
                          <label style="position: relative; left: 105.00px; top: 71.93px; font-size: large;">Most preferred</label>
                  		<input id="Opt" type="text" style="position: relative; width: 276px; height: 28px; left: 401.16px; top: 79.08px;">
                  	</div>
                  
                  	<div>
                          <label style="position: relative; left: 85.00px; top: 120.93px; font-size: x-large; color: #937B62;">Most Likely</label>
                          <label style="position: relative; left: 105.00px; top: 121.93px; font-size: large;">More than likely the case</label>
                          <input id="ML" type="text" style="position: relative; width: 276px; height: 28px; left: 308.88px; top: 124.08px;">
                  	</div>
                  
                  	<div>
                          <label style="position: relative; left: 85.00px; top: 170.93px; font-size: x-large; color: #937B62;">Pessimistic</label>
                  		<label style="position: relative; left: 105.00px; top: 171.93px; font-size: large;">Worst Case</label>
                  		<input id="Pess" type="text" style="position: absolute; width: 276px; height: 28px; left: 640px; top: 451px;">
                      </div>
                  
                  	<div>
                  		<label style="position: relative; left: 85.00px; top: 226.93px; font-size: x-large; color: #937B62;">Result</label>
                  		<input id="Res" type="text" style="position: relative; width: 276px; height: 28px; left: 570.67px; top: 226.35px;" readonly=""  >
                          <object id="divide"></object>
                          <object id="add"></object>
                      </div>
                  
                  	<div style="height: 80.44px;">
                          <button value="Calculate" onclick="estimate()" style="position: relative; width: 165px; height: 54.27px; left: 80px; top: 273.08px; font-size: x-large;" type="button">Calculate</button>
                       
                      </div>
                  
                  	<div style="height: 40px;">
                  		<button id="reset" type="reset" value="Reset" style="position: relative; width: 125px; height: 45px; left: 1010px; top: 243.08px; font-size: x-large;">Reset</button>
                  	</div>
                  
                  </body>
                  </html>

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    You made changes that I didn't say to make. Go back and make only the those two changes I mentioned.

                    Comment

                    • jmrker
                      New Member
                      • Dec 2012
                      • 17

                      #11
                      Please read 'Rabbit's request...
                      Code:
                      <html>
                      <head>
                      <title> </title>
                      <script type="text/javascript">
                          function estimate() {
                            var Opt = parseInt(document.getElementById('Opt').value);
                            var ML = parseInt(document.getElementById('ML').value);
                            var Pess = parseInt(document.getElementById('Pess').value);
                            var Res = ((Opt + (4 * ML) + Pess)/6);
                            document.getElementById("showsum").value= Res.toFixed(2);
                          }
                          </script>
                           
                          </head>
                          <body>
                            <form id="myForm" action="" method="post" onsubmit="return false">
                      
                              <p style="text-align: center; font-size: xx-large; font-style: normal; font-variant: small-caps; font-weight: bold; color: #2D7176;">Pert Calculator</p>
                              <div style="position: relative; width: 955px; left: 285px; top: 0.08px; height: 87.28px;">
                                  <label style="font-size: xx-large; font-style: oblique; text-align: center; width: 1010px; position: relative; left: 0px; top: 0px;">
                                   This PERT application will help you estimate PERT. The formula for PERT is ((Optimistic + (4 * Most Likely) + Pessimistic) / by 6)</label>
                              </div>
                           
                              <div>
                                  <label style="position: relative; left: 85.00px; top: 70.93px; font-size: x-large; color: #937B62;">Optimistic</label>
                                  <label style="position: relative; left: 105.00px; top: 71.93px; font-size: large;">Most preferred</label>
                                  <input id="Opt" type="text" style="position: relative; width: 276px; height: 28px; left: 401.16px; top: 79.08px;">
                              </div>
                           
                              <div>
                                  <label style="position: relative; left: 85.00px; top: 120.93px; font-size: x-large; color: #937B62;">Most Likely</label>
                                  <label style="position: relative; left: 105.00px; top: 121.93px; font-size: large;">More than likely the case</label>
                                  <input id="ML" type="text" style="position: relative; width: 276px; height: 28px; left: 308.88px; top: 124.08px;">
                              </div>
                           
                              <div>
                                  <label style="position: relative; left: 85.00px; top: 170.93px; font-size: x-large; color: #937B62;">Pessimistic</label>
                                  <label style="position: relative; left: 105.00px; top: 171.93px; font-size: large;">Worst Case</label>
                                  <input id="Pess" type="text" style="position: absolute; width: 276px; height: 28px; left: 640px; top: 451px;">
                              </div>
                           
                              <div>
                                  <label style="position: relative; left: 85.00px; top: 226.93px; font-size: x-large; color: #937B62;">Result</label>
                                  <input id="showsum" type="text" style="position: relative; width: 276px; height: 28px; left: 570.67px; top: 226.35px;" readonly=""  >
                           
                              </div>
                           
                              <div style="height: 80.44px;">
                                  <button value="Calculate"  onclick="estimate()"
                                     style="position: relative; width: 165px; height: 54.27px; left: 80px; top: 273.08px; font-size: x-large;" type="button">
                                  Calculate</button>
                           
                              </div>
                           
                              <div style="height: 40px;">
                                  <button id="reset" type="reset" value="Reset"
                                     style="position: relative; width: 125px; height: 45px; left: 1010px; top: 243.08px; font-size: x-large;">
                                  Reset</button>
                              </div>
                            </form>    
                          </body>
                          </html>
                      Note: You need a <form> tag for the 'Reset' to work.
                      Also, your 'showsum' is a .value, not a .innerHTML
                      Finally, no need to pass parameters in the 'estimate()' function.

                      Comment

                      Working...