4
I’m studying Javascript for a Youtube video class I did like the video, but when I click Calculate the result appears and already disappears in less than 1 second, when in fact it should stay in the text field.
Follow the html and js below.
Note: Javascript is in a separate file.
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>Javascript aula 03</title>
<link rel="stylesheet" href="_css/estilo.css">
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="csss/estilo.css">
<script src="js/aula07.js"></script>
</head>
<body>
<img src="imagens/imc.jpg" height="180px" width="200px">
<form id="formulario">
<fieldset style="width:20%;">
<legend>Cálculo do IMC</legend>
<span>
<label for="kilos"> Kilos </label>
<input type="text" name="kilos"><br>
</span>
<span>
<label for="metros"> Metros </label>
<input type="text" name="metros"><br>
</span>
<span>
<label for="centimetros"> Cm </label>
<input type="text" name="centimetros"><br>
</span>
<span>
<label for="imc"> IMC: </label>
<input type="text" name="imc" disabled="disabled"><br>
</span>
<span>
<a href="" onclick="calcularIMC();"> Calcular </a>
</span>
</fieldset>
</form>
</body>
</html>
function calcularIMC() {
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);
}
Ideally you share one Minimum, Complete and Verifiable Example.
– Romulo
In addition to what was answered by @Romulo, there is a syntax error here:
formulario.imc.value = imc;.toFixed(2);
also.– Renan Gomes