/    Sign up×
Community /Pin to ProfileBookmark

Help a non-programmer out

I’m a student who has to do a mandatory course in “information technology” which involves a bit of programming. I’ve managed to get all my scripts to work except for this one. It’s essentially a tiny program that requires you to input your height and weight, and it ought to calculate your BMI. Except, when I input some numbers and press the button, nothing happens. As I said, I barely know anything about programming, so I would prefer your simplest solutions. Thanks in advance!
<!DOCTYPE html>
<html lang=”en” dir=”ltr”>
<head>
<meta charset=”utf-8″>
<style>
#bmi
{
padding: 5px;
width: 150px;
background-color: #ccc;
font-weight: bold;
}
</style>
<script>
document.getElementById(“btBMI”).onclick=function()
{
var v=document.getElementById(“v”).value;
var h=document.getElementById(“h”).value;
var BMI=v/(h/100*h/100);
document.getElementById(“bmi”).innerHTML=bmi.toPrecision(3);
}
</script>
<title></title>
</head>
<body>
<input type=”text” id=”v”> Input your weight in kilograms<br>
<input type=”text” id=”h”> Input your height in centimetersr<br>
<button id=”btBMI”>Calculate BMI</button>
<div id=”bmi”>Your BMI:</div>
</body>
</html>

to post a comment
CSSHTMLJavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 24.2021 — You defined the HTML elements, inputs and button, **after** the script, therefore they are not yet defined when you add the eventlistener for the button. Place the script below, right before the closing `&lt;/body&gt;` and everything will be fine.
Copy linkTweet thisAlerts:
@rpg2009Jan 24.2021 — Right here goes

I have moved your script down to the bottom of the code, just before the end body tag. This gives the page time to load, so that we can then select things with getElementById.

If we left it at the top, there would be nothing in the body and getElementById would return null. There are ways around this using DOMContentLoaded, but trying to keep things simple

Here is the amended code with comments
``<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang='en' dir='ltr'&gt;
&lt;head&gt;
&lt;meta charset='utf-8'&gt;
&lt;title&gt;BMI&lt;/title&gt;
&lt;style&gt;
#bmi{
padding: 5px;
width: 150px;
background-color: #ccc;
font-weight: bold;
}
&lt;/style&gt;
&lt;!-- putting our script here would fail as the page hasn't loaded yet--&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type='number' id='weight'&gt; Input your weight in kilograms&lt;br&gt;
&lt;input type='number' id='height'&gt; Input your height in centimeters&lt;br&gt;
&lt;button id='btBMI'&gt;Calculate BMI&lt;/button&gt;
&lt;div id='bmi'&gt;Your BMI:&lt;/div&gt;
&lt;!-- Put script just before end body tag --&gt;
&lt;!-- That way the content of the page loads and we can select things --&gt;
&lt;script&gt;
document.getElementById('btBMI').onclick = function(event) {
// var is outdated, const(constant) and let are preferred
// value will return a string, so '10' + '15' = '1015'
// using parseFloat we can convert that value to a number
const weight = parseFloat(document.getElementById('weight').value);
const height = parseFloat(document.getElementById('height').value);
const bmi = weight/(height/100*height/100);

document.getElementById('bmi').textContent = bmi.toPrecision(3);
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``

I have changed v and h for English words. Back in the day computer languages were limited to single character names, but with javascript you can use descriptive names, which makes your code easier to understand. Especially when you come back to it a month later and you wonder what the hell v and h are.

Values from inputs are in string form, which doesn't work as expected when doing calculations.

e.g. '10' + '15' = '1015'

You want to convert the values to a number first and so I have used parseFloat. see [link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat)

I have also changed your input from input type='text' to input type='number'. This prevents the user typing in garbage like 'av#@234'
Copy linkTweet thisAlerts:
@cootheadJan 24.2021 — Hi there Nordeps,

and a warm welcome to these forums. ;)

Further to what @Sempervivum has pointed out,

I would suggest that you use [i]input type="number"[/i]

instead of[i]input type="text"[/i]. Also using the [i]label

element[/i]
is considered to be good practise. There

is also no need to use [i] dir="ltr"[/i] as it is the default

setting.

<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;
&lt;title&gt;BMI Calculation&lt;/title&gt;
&lt;!--
The internal CSS should be transferred to an external file
&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;
--&gt;
&lt;style media="screen"&gt;
body {
background-color: #f0f0f0;
font: normal 1em / 1.62em sans-serif;
}

input[type="number"], button {
margin: 0.4em 0;
}

#bmi {
width: 9.4em;
padding: 0.4em;
margin: 0.5em 0;
background-color: #ccc;
font-weight: bold;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;input type="number" id="v" min="40" max="140" value="40"&gt; &lt;label for="v"&gt;Input your weight in kilograms&lt;/label&gt;&lt;br&gt;
&lt;input type="number" id="h" min="120" max="220" value="120"&gt; &lt;label for="h"&gt;Input your height in centimetres&lt;/label&gt;&lt;br&gt;
&lt;button id="btBMI"&gt;Calculate BMI&lt;/button&gt;
&lt;div id="bmi"&gt;Your BMI:&lt;/div&gt;

&lt;script&gt;
(function(d) {
'use strict';
d.getElementById('btBMI').onclick = function() {
var v = d.getElementById('v').value,
h = d.getElementById('h').value,
BMI = v/( h/100 * h/100);
d.getElementById('bmi').textContent = BMI.toPrecision(3);
}
}(document));
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;



[i]coothead[/i]
Copy linkTweet thisAlerts:
@JennieMillerJan 26.2021 — Put the script below.
×

Success!

Help @Nordeps spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...