326 posts
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Referred between 100 and 199 users
  • Bought between 50 and 99 items
  • Has been a member for 3-4 years
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Portugal
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
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Exclusive Author
  • Referred between 10 and 49 users
  • Has been a member for 4-5 years
  • Portugal
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 ;)

326 posts
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Referred between 100 and 199 users
  • Bought between 50 and 99 items
  • Has been a member for 3-4 years
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Portugal
vtimbuc says

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

1675 posts Envato Elite Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
+1 more
Tean says

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

326 posts
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Referred between 100 and 199 users
  • Bought between 50 and 99 items
  • Has been a member for 3-4 years
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Portugal
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 ;)

1675 posts Envato Elite Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
+1 more
Tean says

Maybe like this:

var name = "Envato";

var hello;

$('a').on('click', function () {
    name = "CodeCanyon";
hello = "Hello" + name + "!";
});
326 posts
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Referred between 100 and 199 users
  • Bought between 50 and 99 items
  • Has been a member for 3-4 years
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Portugal
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.

1675 posts Envato Elite Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
+1 more
Tean says

Not sure what you mean, show us more code.

47 posts
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Exclusive Author
  • Referred between 10 and 49 users
  • Has been a member for 4-5 years
  • Portugal
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