简体   繁体   中英

Javascript: why will variable work locally but not globally

I am new to JavaScript and am having trouble understanding why a variable can be used when it is declared inside a function (locally) but not outside (globally).

For example: https://jsfiddle.net/Buleria28/kqu69aqt/

Or if it is easier to view here. Why will this work?:

function numDisplay (){
var e = document.getElementById("numVal").value;
document.getElementById("show").innerHTML = e;
}

document.getElementById("calcBtn").addEventListener("click",numDisplay);

And why won't this work?:

var e = document.getElementById("numVal").value;
function numDisplay (){
document.getElementById("show").innerHTML = e;
}

document.getElementById("calcBtn").addEventListener("click",numDisplay);

The corresponding HTML is:

<form method = "POST">
<fieldset>          
<label for="numVal">Enter Number Value:</label>
<input type="number" id="numVal" name="numVal"/>
</fieldset>
</form> 

I am curious because I would like to use the user input for found in the variable "e" in different functions.

The problem is when the script code runs. If you set a global variable like this and it is null

var e = document.getElementById("numVal").value;

Then it probably executed before the browser created that part of the page.

You can use global variables that you set to a number or string that way since that does not depend on the page / document:

var number_of_puffins = 11;

If you want a variable to point to a part of the document you need to have a function to set it after the page exists.

<body onLoad="setglobals();">

Be careful about using code like your example since the page can change if you add or delete items.

Both of your options work as planned.

The difference is the value.

When you run the script for the first time, there is no value in the input, but when you run it inside the function, it alredy has value.

You can change when you are calling the value, ie:

var e = document.getElementById("numVal"); //Remove the .value
/*why won't having the variable above rather than inside
the function work?*/

function numDisplay (){
//var e = document.getElementById("numVal").value;
document.getElementById("show").innerHTML = e.value; //use it here.
}

document.getElementById("calcBtn").addEventListener("click",numDisplay);

In the case of:

var e = document.getElementById("numVal").value;
function numDisplay (){
   document.getElementById("show").innerHTML = e;
}
document.getElementById("calcBtn").addEventListener("click",numDisplay);

e is computed at run time, so the value is nothing which is then stored as e (your starting value.) However making a slight change to your code can make this work.

var e = document.getElementById("numVal");
function numDisplay (){
   document.getElementById("show").innerHTML = e.value;
}
document.getElementById("calcBtn").addEventListener("click",numDisplay); 

In this example e's value is found on each click, which will then display the current value and set that as the innerHTML of your 'show' element.

The function numDisplay() doesn't work correctly when e is a global variable because numDisplay() is triggered when #calcBtn is clicked.

Let's say you enter the number 5 in the input field and click the "Show Number" button. This runs numDisplay() and sets the HTML in #show to e . However, e never got the number 5. It still has an empty value which was assigned to it when you loaded the page. For it to keep getting new values each time you click the button, var e = document.getElementById("numVal").value; needs to be inside the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM