View Full Version : Update variables onclick

10-21-2009, 08:02 PM
Hi I have simple int variable in javascript.
I want to change it's value when I click on some link.

How can I do it?


10-22-2009, 04:38 AM
There is no such thing (technically speaking) as a simple variable. The closest thing would perhaps be a global variable. If that's what you have, it can be changed in an onclick event without much trouble. Let's say, as could be construed from your post, that the name of this variable is int:

onclick="int = 5;"

Now, of course, if this is a link, one should return false.

In the above I've set it to the number 5. It could just as easily be set to a string, boolean, object, etc.

But, even with a global, if it has already been put into another scope, depending upon the situation (like as part of an interval), though its value will change, its value in the interval will not.

If it is not a global, unless it has some way of being referenced from the global scope (like being a part of an object in the global scope), you might not be able to access it from an onclick event.

So if you are still having problems, please be more specific and post a link to your page where this is an issue.