This might be a dumb question for you advanced developers.
Below is a script on my own website that I want to modify, in order to avoid declaring same variables in each function. I want to avoid declaring variables in the main script structure.
Below code works, still wanted to know if there is a more elegant way to do this.
As Answered below, it's best to enclose everything in an object.
var var1 = document.getElementById("div1");
var var2 = document.getElementById("div2");
function foo1(){
div1.style.display = "none";
}
function foo2(){
div2.style.display = "block";
}
function function?? Also where is this error? jsfiddle.net/rlemon/fxhK7 I do not see it?? Unless you want to pass around the object, in what you have shown me this does use global variables. in JS you do not need to cast them as such. – rlemon Aug 13 '12 at 14:44function function? typo? – PitaJ Aug 13 '12 at 14:45function function. You're probably executing the function before you initialize the variables... I think this should work. – Stephen Sarcsam Kamenar Aug 13 '12 at 14:47function functionthen a rollback again, to PitaJ's removel, when op said it was a typo and I then thought it's was not the meaning of the question. – Andreas AL Aug 13 '12 at 14:51