-1
When I use variables to store content from text boxes, to can execute in a function (just like it is in the code I left here), if the variables are not inside the function, the code does not work correctly. The code below was made to capture the contents of a number text box and send an Alert with what was inside the box, but only works if I put the variables inside the function. I wanted to understand why since the variable is in the global scope.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="number" id="num">
<input type="button" value="Ok" onclick="clicar()">
<script>
var num = document.getElementById('num')
var numero = Number(num.value)
function clicar(){
window.alert(numero)
}
</script>
</body>
</html>
That’s weird, I took a test here and it was no problem. If your Javascript code is exactly the one you put in the question, is that you have not forgotten to call the function no ?
– JeanExtreme002
I think you should try [Edit] your question to add code as text. As can be read here, posting an image as code is not good practice on this site, and your question may be negatively so.
– Luiz Felipe