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

  • SEARCH
  • Home
  • 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 6868971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:30:01+00:00 2026-05-27T03:30:01+00:00

I am new to Javascript. I am facing a problem with global variables. I

  • 0

I am new to Javascript. I am facing a problem with global variables. I can’t figure out that why the global variables are not working as the code looks ok. Please Help me solve this problem.
I will breifly explain the code first.I have some text on a page which changes to text field when clicked. When I define the variables inside the functions body the code starts working fine. When these variables are defined globally as in the following code, the console displays this error: the variable is not defined. Here my code:

<!DOCTYPE HTML>
<html>
<head>
<title>Span to Text Box - Demo - DOM</title>
<script type="text/javascript" language="javascript">
var textNode = document.getElementById('text');
var textValue = textNode.firstChild.nodeValue;
var textboxNode = document.getElementById('textbox');
var doneButton = document.getElementById('done');
function change()
{
   textboxNode.setAttribute('value', textValue);
   textNode.style.display = 'none';
   textboxNode.setAttribute('type','text');
   doneButton.setAttribute('type','button');
}
function changeBack()
{
   textNode.firstChild.nodeValue = textboxNode.value;
   textNode.style.display = 'block';
   textboxNode.setAttribute('type', 'hidden');
   doneButton.setAttribute('type','hidden');
}
</script>
</head>

<body>
<p id="text" onClick="change()">Click me!</p>
<form onSubmit="return false;">
  <input type="hidden" id="textbox" />
  <input type="hidden" id="done" onClick="changeBack()" value="Done" />
</form>
</body>
</html>

Please Help!
Thanks in Advance.

  • 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-27T03:30:01+00:00Added an answer on May 27, 2026 at 3:30 am

    As Adam said, the issue is that you are running javascript on the document before the document has been loaded. There are a number of ways to fix this, but the simplest is to just move your javascript code to the end of the body so the document has already been parsed and is ready before your code runs like this:

    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Span to Text Box - Demo - DOM</title>
    </head>
    
    <body>
    <p id="text" onClick="change()">Click me!</p>
    <form onSubmit="return false;">
      <input type="hidden" id="textbox" />
      <input type="hidden" id="done" onClick="changeBack()" value="Done" />
    </form>
    
    <script type="text/javascript" language="javascript">
    var textNode = document.getElementById('text');
    var textValue = textNode.firstChild.nodeValue;
    var textboxNode = document.getElementById('textbox');
    var doneButton = document.getElementById('done');
    function change()
    {
       textboxNode.setAttribute('value', textValue);
       textNode.style.display = 'none';
       textboxNode.setAttribute('type','text');
       doneButton.setAttribute('type','button');
    }
    function changeBack()
    {
       textNode.firstChild.nodeValue = textboxNode.value;
       textNode.style.display = 'block';
       textboxNode.setAttribute('type', 'hidden');
       doneButton.setAttribute('type','hidden');
    }
    </script>
    
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to JavaScript. I am facing a problem with my javascript code.
I am new to JavaScript OOP. Can you please explain the difference between the
I'm kind of new to JavaScript and jQuery and now I'm facing a problem:
I am facing problem that whenever I turn off security settings in IE8, in
I am completely new to javascript programming and I have a question that I
I'm new to jQuery and am facing now a weird problem. I succeeded to
I am relatively new to javascript and I am facing some difficulty.I have two
I am facing problem for reading xml file using javascript. It works fine in
I'm having a problem with the following code. Below is my javascript <script type=text/javascript><!--
I've just been helping out with some interviews for a new developer and JavaScript

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.