Function scope & variables in/out of said function
<p>So im studying and just trying something out and well, the question is this</p>

<p>i know that a variable defined outside of a function is avail to all while one set inside a function is not.</p>

<p>that said when i do this</p>

<p><strong>HTML</strong>:  </p>

<pre><code>&lt;input type="text" name="field" id="field"/&gt;

&lt;input type="submit" id="submit" name="submit"/&gt;
</code></pre>

<p><strong>JS</strong>:</p>

<pre><code>var results = document.getElementById("field").value;
var submitBtn = document.getElementById("submit");

function pushit(){
    aNames.push(results);
    for(i=0; i&lt;aNames.length; i++){
        document.write(aNames[i] + "&lt;br/&gt;");
    }

    submitBtn.onclick = pushit;
}
</code></pre>

<p>it doesn't work. when submit is hit, it just displays the original array items.</p>

<p>but when i change the function to this: </p>

<pre><code>function pushit(){
    var results = document.getElementById("field").value;
    aNames.push(results);
    for(i=0; i&lt;aNames.length; i++){
         document.write(aNames[i] + "&lt;br/&gt;");
    }
}
</code></pre>

<p>essentially just putting the var results inside the function it works. my ? is why?</p>

<p>if i was to say for example, check in a form for a specific value of a field in the form, id check in the same manner, for the "value" of it against what i wanted to check so...im just not understanding why it doesnt work.</p>

<p>any tips, links, explanations etc...i gladly appreciate.</p>
