2
As can be seen in the code, the variables declared are global, and the routine should display the result on the console as soon as the "Send" 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 running, 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 other javascript lines?
Your onclick performs the function
conta
, Anything you want to do from the click - including logging in results on the console - has to be done inside. The part you called routine runs as soon as the page loads, and right after the account function is created, but before it is executed (it will only run in the click).– bfavaretto