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-12-2012, 02:43 AM   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 using text boxes and linking them back to java script

Hi I recently posted, I'm having trouble taking 2 values from 2 text boxes and sending them to another text box. Im suppose to calculate the BMI without using function arguments and using the parseINT() function.

Here's the 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="mass" action="" method="get">
<p>
Weight<input type="text" value="0" name="weight" onchange="calcBMI(document.mass.weight.value)"/>
<br>
Height<input type="text" value="0" name="height" onchange="calcBMI(document.mass.height.value)"/>
<br>
BMI<input type="text" name="result" value="" readonly>
<br>
<input type="button" name="calc" value="Calculate" onclick="calcBMI()">
</p>
</form>
<script type="text/javascript">
function calcBMI(){
var w = document.getElementsByName('weight');
var h = document.getElementsByName('height');
var final = ( w * 703 / ( h*h ) );
form.result.value = final;
}
</script>
</body>
</html>

Thanks.
CodyJava is offline   Reply With Quote
Old 10-12-2012, 07:58 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are. Study it and learn, please. You need to check that the user enters sensible numbers. Also say what units height and weight are to be in.

You should use Number(), not parseInt() to change the input values (strings) to numbers. But if someone insists on parseInt() (not parseINT) remember that you must specify the radix. parseInt() is really intended for use when converting from one number base to another. In any case perhaps you intend parseFloat().


Code:
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Body Mass Index</h1>
<form id="mass" action="" method="get">
<p>
Weight<input type="text"  id="weight" >
<br>
Height<input type="text" id="height">
<br>
BMI<input type="text" id="result" value="" readonly> 
<br>
<input type="button"  value="Calculate" onclick="calcBMI()">
</p>
</form>
<script type="text/javascript">
function calcBMI() {
document.getElementById("result").value = 0;
var w = Number(document.getElementById('weight').value) || 0;  // trap NaN entries
var h = Number(document.getElementById('height').value) || 0;
var final = ( w * 703 / ( h*h ) );
if (final < 1e20) {  // trap infinity
document.getElementById("result").value = final.toFixed(2);
}
}
</script>
</body>
</html>
Study the difference between getElementsByName() and getElementbyId() The getElementsByName() method accesses all elements with the specified name.

Old Pedant offered you a working script in a previous thread. He seems to have wasted his time as you have pretty much ignored him. There is not much point in asking for help if you disregard the suggestions made.

BTW, re your user name CodyJava. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!




Quizmaster: In which African country did Mahatma Gandhi spend several years as a lawyer?
Contestant: India
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-12-2012 at 08:47 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M 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 02:29 PM.


Advertisement
Log in to turn off these ads.