332 posts
  • Has referred 100+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+5 more
vtimbuc says

Hey guys,

How can I update a global variable on click?

Ex:
var globalVar = "hello envato!";

$('a').on('click', function () {
    // Change Variable value
    var globalVar = "hello again!" 
});

Thank you :)

47 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
eduvoindo says

Just drop the var inside the click function, like this:

var globalVar = "hello envato!";

$('a').on('click', function () {
    globalVar = "hello again!" 
});

That’s because of variable scope ;)

332 posts
  • Has referred 100+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+5 more
vtimbuc says

Thanks Eduardo, but I need this variable to be outside of the click event.

1677 posts Envato Elite Author
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
Tean says

This variable now is pointing to the outside of the click event.

332 posts
  • Has referred 100+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+5 more
vtimbuc says

Yes, I didn’t noticed that he removed the “var”.

I have another question:

How can listen for variable changes, and when the variable changes update another variable.

Ex:
var name = "Envato";

var hello = "Hello" + update + "!";

$('a').on('click', function () {
    name = "CodeCanyon";
});

How can I update the “hello” var to have the new “name” value?

Thanks guys for the help ;)

1677 posts Envato Elite Author
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
Tean says

Maybe like this:

var name = "Envato";

var hello;

$('a').on('click', function () {
    name = "CodeCanyon";
hello = "Hello" + name + "!";
});
332 posts
  • Has referred 100+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+5 more
vtimbuc says

Isn’t there any other way to update this variable without including it on the click event. Because the variable contains a function with 50 lines of code, and duplicate it on the click event is not a good option.

1677 posts Envato Elite Author
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
Tean says

Not sure what you mean, show us more code.

47 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
eduvoindo says

That’s a little bit more tricky, you will probably need to use objects instead of variables, like this:

var names = {
    name: "Envato" 
}

var hello = "Hello " + names.name + " !"; // Initial value for hello now containes "Hello Envato !" 

var set = function(obj, new_value) {
    obj.value = new_value; // changes the object dinamically
    hello = "Hello" + names.name + "!"; // And update the var hello, so it reflects the change ;)
}

// And now you can do:
set(name, "CodeCanyon");

// if you test it nowm it will be updated
alert(hello); // this will alert "Hello CodeCanyon !" 
by
by
by
by
by
by