Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-11-2012, 10:21 PM   PM User | #1
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
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.
CodyJava is offline   Reply With Quote
Old 10-11-2012, 10:24 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Users who have thanked WolfShade for this post:
CodyJava (10-11-2012)
Old 10-11-2012, 10:29 PM   PM User | #3
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
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.
CodyJava is offline   Reply With Quote
Old 10-11-2012, 11:52 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
CodyJava (10-12-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:02 AM.


Advertisement
Log in to turn off these ads.