Sunday, June 2, 2024
153
rated 0 times [  156] [ 3]  / answers: 1 / hits: 20984  / 15 Years ago, thu, march 25, 2010, 12:00:00

I'm sure this is really simple and I'm drawing a giant blank, but how do you set the result of a function as a global variable?



Example, I want to set the first color in array colors as global variable color (I know the example doesn't make much practical sense, but it's just to illustrate my question):



var color = ;

function selectColor () {
var colors = [blue,red,green,yellow];
var color = colors[0];
return color;
}

window.onload = function () {
selectColor ();
alert(color);
}

More From » global-variables

 Answers
12

It should work for you if you remove the var declaration from color in the selectColor() function, like this:



var color = ;

function selectColor () {
var colors = [blue,red,green,yellow];
color = colors[0];
return color;
}

window.onload = function () {
selectColor ();
alert(color);
}

[#97243] Monday, March 22, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rhiannab

Total Points: 370
Total Questions: 98
Total Answers: 100

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
;