0

I am trying to get the document.bmi.answer.value to equal to ans. But it is not showing up. Does anyone see any errors in my code?

This is in my head:

<script type="text/javascript">
function calcbmi()
{
var hh = parseFloat(document.getElementByName('height').value);
var ww = parseFloat(document.getElementByName('weight').value);
var ans = (( ww / ( hh * hh ))*703);
document.getElementByName('answer').value = ans;
}
</script>

This is in my body:

<form name="bmi">
<p> Height <input type="text" name="height"></p>
<p> Weight <input type="text" name="weight"></p>
<p> BMI <input type="text" name="answer" value=""></p>
<input type="button" value="Calculate" onclick="calcbmi();">
</form>

Thanks for the help!

Here is my whole code as my error my not reside in the javascript or form. I cant for the life of me see it though..

<!DOCTYPE HTML>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function calcbmi()
{
var hh = parseFloat(document.getElementByName('height').value);
var ww = parseFloat(document.getElementByName('weight').value);
var ans = (( ww / ( hh * hh ))*703);
document.getElementByName('answer').value = ans;
}
</script>
</head>
<body>
<h1>Body Mass Index</h1>
<form name="bmi">
<p> Height <input type="text" name="height"></p>
<p> Weight <input type="text" name="weight"></p>
<p> BMI <input type="text" name="answer" value=""></p>
<input type="button" value="Calculate" onclick="calcbmi();">
</form>
</body>
</html>
4
  • What you're talking about is JavaScript, not Java the server-side programming language. Commented Apr 17, 2013 at 16:51
  • Looks like you have an extra ) at the end of var ans = ( ww / ( hh * hh ))*703); Commented Apr 17, 2013 at 16:56
  • Thanks! I fixed it but it still has not solved my over all problem of my answer not showing up in the BMI box. Commented Apr 17, 2013 at 17:00
  • Now your fix has introduced a new bug (I've addressed this in my answer) Commented Apr 17, 2013 at 17:06

3 Answers 3

1

There is a bug in your JavaScript that is preventing it from executing (here is a demo)

function calcbmi() {
    var hh = parseFloat(document.bmi.height.value);
    var ww = parseFloat(document.bmi.weight.value);
    var ans = (ww / (hh * hh)) * 703;
    document.bmi.answer.value = ans;
}

Remove the extra ) and you should be good

Now that you've updated your code to fix the parenthesis you have introduced a new bug, the command you want (assuming you want to use the name attribute) is .getElementsByName() (not .getElementByName())

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! Thank you so much! But what was the bug?
Ok got it! Thank you for your help! haha such a simple mistake I over looked.
Your first draft just had the extra ), otherwise it was perfect (you should use Firefox+Firebug or Chrome to develop JavaScript it will tell you when there are syntax errors and exactly what they are)
0

Use

document.getElementByName('answer').value = ans;

2 Comments

I tried this and the answer still does not show up in the BMI box
The function is called getElementsByName(), because it can return more than one: developer.mozilla.org/en-US/docs/DOM/document.getElementsByName
0

You need to use document.getElementById() to get value of an HTML element, Change this:

document.bmi.answer.value = ans;

To:

document.getElementById('answer').value = ans;

This should work.

2 Comments

thanks for the quick response. I changed it but the answer still does not show up in the BMI input box.
@user2291730, it's not getElementByName it should be getElementById() see my answer.. :) and you don't have id attribute for answer, add it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.