Good evening, folks.
I've been studying the foundations of JS and i got stuck with the following code, where the HTML page is supposed to receive a value in metters:
<!DOCTYPE html>
<head>
<meta charset="utf8">
</head>
<html>
<title>Exercícios js</title>
<body>
<p>Insira os dados: </p> <input type="text" name="dados" id="dados" placeholder="Digite a altura em metros">
<button type="button" id="btn-click" onclick="conversor()">Converter !</button>
<script type="text/javascript" src="scripts/motor.js">
document.getElementById("btn-click").addEventListener("click");
</script>
</body>
</html>
And with a function called conversor()
within a separate .js, it's supposed to return the value in feet:
var pes = (document.getElementById("dados")) * 0.305;
function conversor(){
alert('A altura em metros é: ' + (pes)). parseFloat(value);
}
The page alert says 'undefined'. My clue is that I'm trying to retrieve text inputs and then multiply it to a floating point number. You can see it by my attempt of using parseFloat...
That's the only clue I had. What is wrong with the above code?
答案 0 :(得分:0)
There are some mistake in your code:
Did not set event handle to click event.
document.getElementById("btn-click").addEventListener("click", conversor);
Did not get value of document.getElementById("dados").value
0(document
may be your typo.
function conversor(){
var pes = (document.getElementById("dados").value) * 0.305;
alert('A altura em metros é: ' + (pes));
}
<!DOCTYPE html>
<head>
<meta charset="utf8">
</head>
<html>
<title>Exercícios js</title>
<body>
<p>Insira os dados: </p> <input type="text" name="dados" id="dados" placeholder="Digite a altura em metros">
<button type="button" id="btn-click" onclick="conversor()">Converter !</button>
<script type="text/javascript" src="scripts/motor.js">
document.getElementById("btn-click").addEventListener("click", conversor);
</script>
</body>
</html>