When the printNumero
function is called, the local variable numero
does what I wanted, returns the value that was entered in the input. But the variable numero
global returns an empty string, even though I have put a value in the input of it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>bingo.html</title>
</head>
<body>
<input type="text" id="numero">
<input type="submit" id="verificar" value="Adicione e verifique no Bingo!">
<script>
var numero = document.getElementById("numero").value;
function printNumero() {
var numero = document.getElementById("numero").value;
return numero;
};
var verificar = document.getElementById("verificar");
verificar.addEventListener("click", function() {
console.log(numero);
});
</script>
</body>
</html>
EDIT
Why does the variable numero
local (the one of the function) return the value of the input and the variable numero
global returns an empty string? Remembering, without the help of the function, is the same global variable.