Doctor Colosssu
01-17-2009, 05:17 AM
This code relates to some display = 'none' / display = 'block' CSS-toggled calendars I have. Since they're visible by default, in case Javascript is disabled, I'm adding the "show calendar/hide calendar" link dynamically, since it would be useless to someone without Javascript.
My trouble is this: I'm trying to loop through each calendar and add a unique toggler for each, but I can't figure out how to correctly pass unique parameters for each onclick event. What's happening is that when onclick is executed for any of the togglers outputted, it uses the variable reference I specify, when what I'm aiming at, rather, is to pass the variable's value at each iteration of the for loop instead. Is my English making sense?
Below is my code, with the problematic line emboldened. calz stands for calendars.
var calz = document.getElementsByTagName('table');
for (var i=0; i<calz.length; i++) {
if (calz[i].className != 'calendar') continue;
var anchor = document.createElement('a');
anchor.setAttribute('href', 'javascript:void(0)');
anchor.id = calz[i].id + 'Toggle';
anchor.onclick = function(){toggle(calz[i])};
var img = document.createElement('img');
img.setAttribute('src', 'calendar.jpg');
anchor.appendChild(img);
anchor.appendChild(document.createTextNode(' [show calendar]'));
calz[i].parentNode.insertBefore(anchor, calz[i]);
}
Thanks for any help! I'm a newb with Javascript.
Doctor Colossus
My trouble is this: I'm trying to loop through each calendar and add a unique toggler for each, but I can't figure out how to correctly pass unique parameters for each onclick event. What's happening is that when onclick is executed for any of the togglers outputted, it uses the variable reference I specify, when what I'm aiming at, rather, is to pass the variable's value at each iteration of the for loop instead. Is my English making sense?
Below is my code, with the problematic line emboldened. calz stands for calendars.
var calz = document.getElementsByTagName('table');
for (var i=0; i<calz.length; i++) {
if (calz[i].className != 'calendar') continue;
var anchor = document.createElement('a');
anchor.setAttribute('href', 'javascript:void(0)');
anchor.id = calz[i].id + 'Toggle';
anchor.onclick = function(){toggle(calz[i])};
var img = document.createElement('img');
img.setAttribute('src', 'calendar.jpg');
anchor.appendChild(img);
anchor.appendChild(document.createTextNode(' [show calendar]'));
calz[i].parentNode.insertBefore(anchor, calz[i]);
}
Thanks for any help! I'm a newb with Javascript.
Doctor Colossus