I am trying to perform two tasks on a "toggle collapse" button (which is actually an image):
- If a section of the page is loaded collapsed (from cookie) then add the class "collapsed" to an ancestor tag.
- Add an event handler to toggle the presence of the "collapsed" class when the button is clicked.
To do so, I have the following code:
function toggleCollapsedPlugin(sender, collapseState) {
    // do something
    // expects 1 OR 2 arguments
}
function initCollapsedPlugin() {
    var forumcats = document.getElementById('forums').getElementsByClassName('forumbit_nopost L1');
    var button;
    var buttonParent;
    for (var i = 0; i < forumcats.length; i++) {
        button = forumcats[i].getElementsByTagName('a')[1].getElementsByTagName('img')[0]; // will always exist
        buttonParent = button.parentNode.id; // will never be empty
        if (button.src.indexOf('_collapsed') >= 0) {
            toggleCollapsedPlugin(button.parentNode.id, true);
        }
        button.addEventListener('click', function () { toggleCollapsedPlugin(buttonParent); }, false);
    }
}
As I step through initCollapsedPlugin() for the first item in the Chrome DevTools, everything appears to be fine. When I later click one of the images to call toggleCollapsedPlugin(sender), sender is always equal to buttonParent of the last item in forumcats. Can someone explain why and how to fix this?
(If it helps anyone provide an answer, this is the vBulletin4 software.)
 
     
     
     
     
    