As can be seen in the code, the declared variables are global, and the routine should display the result on the console as soon as the "Submit" button was selected.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="button" value="Enviar" onclick="conta()">
<script>
//VARIÁVEIS
var numero = 5;
var resultado;
var pronto = false;
//FUNÇÕES
function conta(){
resultado = numero + 5;
pronto = true;
}
//ROTINA
if (pronto == true){
console.log(resultado);
}
</script>
</body>
</html>
But when it is run, the console is not displayed. If variables are declared locally within function conta()
, the operation is performed (which can be proven with a console.log(resultado)
within the function. But this does not affect the console.log(resultado)
external to it.
What should I do to make this function invoked by onclick
interact with the other javascript lines?