1
When the function printNumero
is called, the local variable numero
does what I wanted, returns the value that was inserted in the input. But the variable numero
global returns an empty string, even though I have put a value in its input.
<!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 the variable numero
local(a of the function) can return the input value and the variable numero
global returns an empty string? Remembering, without the help of the function, is the same global variable.
Thanks, but, what if the function did not exist and I used only the global variable I declared, what would be necessary to do so that the value does not return an empty string, or, there is no way?
– Thomas Alves
@Thomasdota, I edited the answer ;)
– Samir Braga
Thank you so much for your help :)
– Thomas Alves