1

I have a global variable "theClass" that I try to assign the contents of a DOM element after a click event has occurred, then I try to use it on another page, but it's not even being assigned in the first place! This' the code, that returns ' false' in the console. I'm using jQuery.

var theClass = '';
var getCategories = false;

$(document).ready(function(){

  $(document).on("click", ".nav li a", function() {
    theClass = $(this).html();
    getCategories = true;
  });

  console.log(theClass+" "+getCategories);
});
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Vincent Hokie
  • 61
  • 2
  • 8

2 Answers2

0

Going by your code, I am assuming by another page you actually mean another place in your code. Your console log gets fired in the ready function, however, you are setting/updating the values in a function. Hence, your logging will not show result as expected.

You can try logging in the event based function only. And will see that the first log gives default values and then subsequent clicks give updated values.

You can try something like following

 $(document).on("click", ".nav li a", function() {
console.log(theClass+" "+getCategories);
theClass = $(this).html();
getCategories = true;

});
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
  • Thanks guys, just saw a post talking about 'localStorage' and that seems to work pretty well! Thanks though! – Vincent Hokie Sep 19 '15 at 16:18
  • @VincentHokie - Good your problem got resolved, however, I would suggest you to understand the concepts as well. – Nikhil Aggarwal Sep 19 '15 at 16:21
  • from your answer, I seem to understand that the reason the updating and logging values are unchanged is because they're inside the 'ready' function..true? – Vincent Hokie Sep 19 '15 at 16:55
  • @VincentHokie - In your case, yes, however, it is not necessary. The simple problem is that you are accessing before updating. – Nikhil Aggarwal Sep 19 '15 at 16:57
  • Ohhhhhh! Because, the logging is fired immediately the ready function is complete! Before which, there are no values.. – Vincent Hokie Sep 19 '15 at 17:01
  • I get that. But, I had multiple links, and I had a scenario where I set them from one click, (got nothing on the log ofcourse) but when I clicked a second link(at this point the variable should be set to the value of the first link) it still gave me undefined.. – Vincent Hokie Sep 19 '15 at 17:05
  • @VincentHokie - Does clicking on anchor reloads the page or take you to some other page, if yes, then it will give undefined only. – Nikhil Aggarwal Sep 19 '15 at 17:15
  • Takes you to another page. So would you say localStorage is the best solution now? – Vincent Hokie Sep 19 '15 at 17:30
0

If you trying to set any value in global variable then it is valid in particular session,when I try to use it on another page then variable got initialize with global value as false.

Prefer below post for finding a another way.

How can I pass values from one html page to another html page using javascript

Community
  • 1
  • 1