Solved

javascript global variable not updating

Posted on 2014-09-28
3
313 Views
Last Modified: 2014-09-29
hello experts

I have
var myGlobalvar;

var addRow= function(view){
myGlobalvar = null;
...
...

myGlobalvar = view;
}

Open in new window

When addRow function is invoked first time it works fine. i.e i am able to see 'view' variable value assigned to
myGlobalvar. (myGlobalvar = view;)

But when addRow function is invoked second time, myGlobalvar still has reference to old value of 'view' variable , i.e myGlobalvar is not getting updated with new value of view.

Any clue what i am doing wrong?

Thanks.
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 400 total points
ID: 40349159
replace :
var myGlobalvar;
by :
window.myGlobalvar;
0
 
LVL 43

Expert Comment

by:Rob
ID: 40349160
Impossible to say from the scope of code you've provided as it looks ok to me.

Try debugging with your browser's dev tools (F12 or Ctrl+Shift+i) and set a breakpoint inside that function.

Inspect the locals on the first and subsequent calls to the function to make sure that the "view" argument is in fact a new value as well as how the global variable is being updated.
0
 
LVL 43

Assisted Solution

by:Rob
Rob earned 100 total points
ID: 40349162
Following on from Leakim971's posts, it's a good idea to use a namespace (I touch on it in my article "Namespaces" in my article: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_13138-Javascript-is-just-an-Object.html)

//var myproject = {}; // empty object

var addRow= function(view){
myproject.myGlobalvar = null;
...
...

myproject.myGlobalvar = view;
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP sending two datas 2 23
Use "if not" in a condition 2 19
Table doesn't show the lines! 3 25
How do I Enable submit button only if listbox has items 4 35
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question