1
My html code is as follows::
<!DOCTYPE html PUBLIC "-//WC3//DTD XHTML 1.0 Stric//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript Aula 07</title>
<script type="text/javascript" src="js/aula07.js"></script>
<style type="text/css">
img, table { width:165px; }
fieldset { width:140px; }
label { display:block; float:left;}
label, input {width:68px; margin:3px 0; }
th, td {border:1px solid #ccc; font-size:0.8em;}
</style>
</head>
<body>
<img src="img/imc.png" alt="imc"/>
<form id="formulario">
<fieldset>
<legend>Cálculo do IMC</legend>
<label for="kilos">Quilos:</label>
<input type="text" name="quilos"/>
<label for="metros">Metros:</label>
<input type="text" name="metros"/>
<label for="centimetros">Cm:</label>
<input type="text" name="centimetros"/>
<label for="imc">IMC:</label>
<input type="text" name="imc" disabled="disabled"/>
<a href="#" onclick="calcularIMC()">Calcular</a>
</fieldset>
</form>
</body>
</html>
And Javascript is:
calcularIMC = function (){
var formulario = document.getElementById("formulario");
var kilos = +formulario.kilos.value;
var metros = +formulario.metros.value;
var centimetros = +formulario.centimetros.value;
var altura = (metros*100 + centimetros)/100;
var imc = kilos / (altura * altura);
formulario.imc.value = imc.toFixed(2);
}
When using Notepad++, the HTML page does not seem to be "calling" Javascript, causing no results to appear in the IMC field. Grateful to those who can help.
Make sure your
<script type="text/javascript" src="js/aula07.js"></script>
has the correct path to the file containing the IMC calculation– CesarMiguel
A good way to see if the js file is being loaded correctly is to go to the source of the page (View page source) and click on
src="js/aula07.js"
. If you open a tab in your browser with your js document it is because it is loading well– CesarMiguel
Friend, your code is working, see it working here, must be the script path:
<script type="text/javascript" src="js/aula07.js"></script>
that is incorrect as quoted @Cesarmiguel. Check the path.– Fernando Leal
Your script has only what you put here or has more code?
– Sergio
Thanks, guys, I got it sorted.
– sscarvalho
@sscarvalho the answer accepted is the solution of the problem? If it is not can answer you. So it is more useful for those who see this question and have similar problem.
– Sergio
It’s not really about the doubt, I’ll just comment with that link because your BMI formula has gotten really weird for me.
– Bruno Augusto