473,394 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Javascript Global Variables Not Working as expected

Hi all,
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:
Expand|Select|Wrap|Line Numbers
  1.  <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Span to Text Box - Demo - DOM</title>
  5. <script type="text/javascript" language="javascript">
  6. var textNode = document.getElementById('text');
  7. var textValue = textNode.firstChild.nodeValue;
  8. var textboxNode = document.getElementById('textbox');
  9. var doneButton = document.getElementById('done');
  10.  
  11. function change()
  12. {
  13. textboxNode.setAttribute('value', textValue);
  14. textNode.style.display = 'none';
  15. textboxNode.setAttribute('type','text');
  16. doneButton.setAttribute('type','button');
  17. }
  18. function changeBack()
  19. {
  20. textNode.firstChild.nodeValue = textboxNode.value;
  21. textNode.style.display = 'block';
  22. textboxNode.setAttribute('type', 'hidden');
  23. doneButton.setAttribute('type','hidden');
  24. }
  25. </script>
  26. </head>
  27.  
  28. <body>
  29. <p id="text" onClick="change()">Click me!</p>
  30. <form onSubmit="return false;">
  31.   <input type="hidden" id="textbox" />
  32.   <input type="hidden" id="done" onClick="changeBack()" value="Done" />
  33. </form>
  34. </body>
  35. </html>
  36.  
Please Help!
Thanks in Advance.
Nov 26 '11 #1
1 3062
omerbutt
638 512MB
Hi Zulqarnain
this is because the variables are initializing before the html document is rendered and thats why it does not find the elements and produce error , where as when you initialize them inside the change()
function the document is already loaded and works , try this code it works as you like it to work
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Span to Text Box - Demo - DOM</title>
  6. <script type="text/javascript">
  7.  
  8.     var textNode =null;
  9.     var textValue =null;
  10.     var textboxNode =null;
  11.     var doneButton =null;
  12.  
  13.     window.onload=function (){
  14.         textNode = document.getElementById('text');
  15.         textValue = textNode.firstChild.nodeValue;
  16.         textboxNode = document.getElementById('textbox');
  17.         doneButton = document.getElementById('done');
  18.     }
  19.  
  20.  
  21.     function change(){
  22.     textboxNode.setAttribute('value', textValue);
  23.     textNode.style.display = 'none';
  24.     textboxNode.setAttribute('type','text');
  25.     doneButton.setAttribute('type','button');
  26.     }
  27.  
  28.  
  29.     function changeBack()
  30.     {
  31.     textNode.firstChild.nodeValue = textboxNode.value;
  32.     textNode.style.display = 'block';
  33.     textboxNode.setAttribute('type', 'hidden');
  34.     doneButton.setAttribute('type','hidden');
  35.     }
  36. </script>
  37.  
  38. </head>
  39.  
  40. <body>
  41. <div>
  42. <p id="text" onClick="change()">Click me!</p>
  43. <form onSubmit="return false;">
  44. <input type="hidden" id="textbox" />
  45. <input type="hidden" id="done" onClick="changeBack()" value="Done" />
  46. </form>
  47. </div>
  48. </body>
  49. </html>
  50.  
  51.  
Nov 27 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
35
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation)...
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
by: phoenix917 | last post by:
I have been working on this code for 5 days... I have read multiple tutorials and looked at various code. This is my third forum try... :) My instructor refuses to help me. I think it's because...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
2
by: localp | last post by:
i created a javascript function that would calculate the total values of an array . (just addition) - and it did work and display correctly in that function. i initialized the variable where i...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.