Changing the value of global javascript variables
I'm having trouble finding out how to change the value of a javascript global variable from within another javascript function. Here's what I've been doing so far.
1/ I set the value in the header as follows:
<script type="text/javascript">
var style = 0;
</script>
2/ I have a function in the header which I want to use to change the value of the global variable
<script type="text/javascript">
function updateStyle(x){
if (x==1) { style = 1; }
if (x==2) { style = 2; }
if (x==3) { style = 3; }
if (x==4) { style = 4; }
alert("style is: " + style);
}
</script>
3/ Then I call the function using onclick.
onclick="updateStyle(1)"
I can see from the 'alert' in the function that the function is being accessed and the value of 'style' within the function is being altered, but it doesn't seem to be the global variable that is being altered. Presumably it is a local version.
The aim is to be able to use the gobal variable's value in the body of the page, but when I examine it, it is always the value set when the global was initialised (ie it is always 0).
At this stage, the code in the body is simply an alert to see what the value is:
<script type="text/javascript">
alert("style is: " + style);
</script>
Any ideas?
Richard
