Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7577283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:08:44+00:00 2026-05-30T17:08:44+00:00

So im studying and just trying something out and well, the question is this

  • 0

So im studying and just trying something out and well, the question is this

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

that said when i do this

HTML:

<input type="text" name="field" id="field"/>

<input type="submit" id="submit" name="submit"/>

JS:

var results = document.getElementById("field").value;
var submitBtn = document.getElementById("submit");

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

    submitBtn.onclick = pushit;
}

it doesn’t work. when submit is hit, it just displays the original array items.

but when i change the function to this:

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

essentially just putting the var results inside the function it works. my ? is why?

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.

any tips, links, explanations etc…i gladly appreciate.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:08:45+00:00Added an answer on May 30, 2026 at 5:08 pm

    The results variable in the first example, is set when the page loads, and is never changed. Each time you read it, it’s the same value.

    In the second example, you are getting the value from the input each time the event is ran; you are making a new variable. Thus, you are seeing the new values.

    JavaScript DOM elements are “live” variables, but strings aren’t.

    For example:

    var results = document.getElementById("field");
    var submitBtn = document.getElementById("submit");
    
    function pushit(){
        alert(results.results);
    }
    

    This works as results is a “live” variable. When you access it, it’s always the latest DOM element.

    When you do var results = document.getElementById("field").value;, now results is just a string; it’s not “live” anymore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been studying this for hours (2 days, actually) and just cannot figure out
I just saw this code while studying the wordpress source code (PHP), You can
I am studying REST with JAX-RS, and this doubt just came to my mind:
Total noobie question here. I'm just learning Java, and studying passing arguments to functions.
I've just recently been studying JQuery to use on a personal website. Something I
I just found out that Google recently decided to start using their own title
I was just studying OCPJP questions and I found this strange code: public static
I'm studying Enterprise Patterns and I'm reading this book . I just read chapter
I'm studying (well, trying to) C right now, but I'm limited to working in
I'm new to using vectors...I just started studying them today. I'm trying to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.