In the following script I'm trying to set-up an 'addEvent()' for the tabs
but I'm getting an error about an 'uncaught exception: ...' in FF error console.
I'm pretty sure it is in the initialization during the onload function, specifically at:
But I don't know how to specify the function correctly so that it creates a 'toggle("#")' for each tab. [toggle("0") ... toggle("4")]
There are commented 'alert(info)' messages scattered about while I was trying to debug this problem.
Can someone show me how to correctly specify the function for the 'addEvent()'.
There is a reference to the original addEvent() code in the script.
Problem is that your scope needed to change. We do this by wrapping your addEvent into an anonymous function.
Thank you very much. That change did the trick. Works like a champ nowl
One last question(s): I would have NEVER thought of that solution.
Can you explain what is happening with the (function(i){ ... statement
that you put inside the onload loop?
Are you creating a function that is part of the body or the window?
Is that the scope that you are referring to?
I see that it works, but I would like to understand why!
The issue is that variable "i" is part of onload function scope. So when you tried to execute it before, the value of "i" was set to the last known value which would be the highest index reached.
In order to address that issue, you need to define a "new scope". You do this by wrapping your code within an anonymous function and pass the variable into it.
It took me a long time to understand this concept as well.
Here is a minimal breakdown:
Code:
(function(i){ // creating new scope -make sure to specify params being passed
addEvent(document.getElementById('sm'+i),'click', function(){toggle(i)});
})(i); // the last parenthetical collection contains the variable to pass
It is just like calling a function:
dosomething(i)
except the function has no name (anonymous), so we represent that with
(function(i){})(i)
So in place of the function name (dosomething), we have (function(i){}) and all the processing goodies inside.
Bookmarks