0
I’m starting with Js on the technician and I’m trying to get the value of the textbox by document.getElementById
and then do operations to return the value by alert
but without success. Follows code:
<html>
<head>
<title></title>
<meta charset="UTF-8">
<script language="Javascript">
var kminicial = parseInt(document.getElementById("1").value);
var kmfinal = parseInt(document.getElementById("2").value);
var abast = parseInt(document.getElementById("3").value);
var dist=kmfinal-kminicial;
var mediakm=dist/abast;
function calcula() {
alert("A distância percorrida é: " + dist + "\n" + "A média de km por litros é:" + mediakm);
}
</script>
</head>
<body>
<p> Quilometragram Inicial: <input type="text" name="kminicial" id="1">
<p> Quilometragram Final: <input type="text" name="kmfinal" id="2">
<p> Litros Abastecidos: <input type="text" name="abast" id="3">
<br><br><br><input type="button" name="calcula" value="Calcular" onclick="calcula()">
</body>
Declare all variables within the function, which will probably work.
– Felipe Duarte