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 6946951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:37:52+00:00 2026-05-27T13:37:52+00:00

I have a problem with a javascript global variable, let’s suppose I have the

  • 0

I have a problem with a javascript global variable, let’s suppose I have the following

var globalVar;

I have a text field

<input type="text" id="myInput" onkeydown="checkKey(this)" onkeypress="checkKey(this)"/>

And in the function checkkey, I assign some value to it:

globalVar=document.forms[0].elements("myInput").value;

I checked the the global variable value has changed with an alert.
When I try to use globalVar in another function, I find it has the value undefined. The other function has something like:

param += globalVar;
  • 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-27T13:37:53+00:00Added an answer on May 27, 2026 at 1:37 pm

    The elements property of form instances isn’t a function, it’s an object. Use

    globalVar=document.forms[0].elements.myInput.value;
    

    …or

    globalVar=document.forms[0].elements["myInput"].value;
    

    …but better yet, with the way you’re calling checkKey, just declare an argument called input or whatever and use

    globalVar=input.value;
    

    …since you’re passing this into checkKey.

    If you’re going to access the form field by its name as you have, use the name attribute rather than the id attribute (although id will work on many browsers as well).

    If your var globalVar; is at global scope (outside of all functions), then it is a global variable. If that var statement is inside a function, it’s a local variable. You haven’t given us enough context to know where the var is, but if it’s at global scope, and you successfully set its value, and later you check it, it will be set.

    Live demo using the long form | Live demo using the argument

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

Sidebar

Related Questions

I have the following problem: <script type=text/javascript> alert(1. ČĆŽŠĐčćžšđ); </script> <script type=text/javascript src=Tst.js></script> <script
I have this html code <html> <head> <title>JQuery Problem 2</title> <script type=text/javascript src=jquery-1.4.min.js></script> <script
Hi I have a problem with a javascript string var foo = \<a href=javascript(foo('a','b'))>test</a>\
I have problem in some JavaScript that I am writing where the Switch statement
I have a problem with a javascript error: $(#slider) is undefined How can i
I have a problem with a javascript set of functions that I made. This
I have problem with dynamically created image (JavaScript). I want to change the innerHTML
So I have a rather basic javascript problem which I have been slamming my
I am having a problem accessing the ViewData object through javascript. I have set
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow

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.