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 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Need help with returning value from function

    Hi my problem is that I am suppose to calculate the BMI from two text boxes while using a function to do it. I'm having trouble returning the value from the function. I'm new to javascript and I'm not sure if there's any other problems with my code.

    Here's the code.
    <!DOCTYPE HTML>
    <html>
    <head>
    <title></title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    function calcBMI(){
    var temp =(weight*703)/(height*height);
    var final = parseInt(temp);
    document.getElementById('finalbmi').value = calcBMI;
    }
    </script>
    </head>
    <body>
    <h1>Body Mass Index</h1>
    <form name="bmi" action="mailto:codymhorton@hotmail.com" method="get">
    <p>
    Weight<input type="text" value="0" name="weight" onchange="calcBMI(document.bmi.weight.value)">
    <br>
    Height<input type="text" value="0" name="height" onchange="calcBMI(document.bmi.height.value)">
    <br>
    BMI<input type="text" id="finalbmi" name="result" value=""> <!-- trying to output value here-->
    <br>
    <input type="button" value="Calculate" onclick="calcBMI();">
    </p>
    </form>
    </body>
    </html>

    Any help would be appreciated, thanks.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    You're calling the function from within the function. Or at least attempting to.

    Use the getElementById to set the value of final, not calcBMI.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    CodyJava (10-11-2012)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Okay if I understand you correctly I need to change

    document.getElementById('finalbmi').value = calcBMI;

    to

    document.getElementById('finalbmi').value = final;

    I did this and it still doesn't display a number is there something else wrong with the code?

    Thanks.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,616
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title></title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <h1>Body Mass Index</h1>
    <form id="bmi" action="mailto:codymhorton@hotmail.com" method="get">
    <p>
    Weight<input type="text" value="0" name="weight"/> (in pounds)
    <br>
    Height<input type="text" value="0" name="height"/> (in inches)
    <br>
    BMI<input type="text" name="result" value="" readonly>
    <br>
    <input type="button" name="calc" value="Calculate">
    </p>
    </form>
    
    <script type="text/javascript">
    (
      function( )
      {
          var form = document.getElementById("bmi");
          form.weight.onchange = calcBMI;
          form.height.onchange = calcBMI;
          form.calc.onclick = calcBMI;
    
          function calcBMI()
          {
               var w = parseFloat(form.weight.value);
               var h = parseFloat(form.height.value);
               var final = "";
               if ( ! isNaN(w) && ! isNaN(h) && h != 0 )
               {
                   final = ( w * 703 / ( h*h ) ).toFixed(0);
               }
               form.result.value = final;
         }
      }
    )( );
    </script>
    
    </body>
    </html>
    That's called "unobtrusive" JavaScript.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    CodyJava (10-12-2012)


  •  

    Posting Permissions

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