So, if declaring the counter using var makes it local to the function, would this be the correct way to initialize it in the following code snippet?
if (typeof counter == 'undefined') var counter = 0;
var myDiv = document.getElementById(bar);
//additional code follows...
The idea being, as the function is called, it initializes the variable if it hasn't been called before. Otherwise, increment the variable by one.