Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:58:31+00:00 2026-06-07T14:58:31+00:00

I am trying to perform two tasks on a toggle collapse button (which is

  • 0

I am trying to perform two tasks on a “toggle collapse” button (which is actually an image):

  1. If a section of the page is loaded collapsed (from cookie) then add the class “collapsed” to an ancestor tag.
  2. 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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:58:32+00:00Added an answer on June 7, 2026 at 2:58 pm

    This is a classic ‘pitfall’ of JavaScript closures. Basically, your buttonParent variable is being overwritten in every iteration and the anonymous function you’re passing to addEventListener references but does not copy this variable.

    You’ll probably need something similar to:

    button.addEventListener('click', (function (parent) {
        return function() {
            toggleCollapsedPlugin(parent);
        };
    })(buttonParent), false);
    

    For a more detailed explanation, see How do JavaScript closures work?.

    In your case however, you can simply get rid of the closed variable altogether and find the button’s parent when the anonymous function is called. This works because this inside of the callback function refers to the element the event was registered on, being the element references by button in that iteration.

    button.addEventListener('click', function() {
        return toggleCollapsedPlugin(this.parentNode.id);
    }, false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to perform a query which is composed of two $or 's:
I am trying to perform a SQL query which generates a table with two
I'm having some problems trying to perform a query. I have two tables, one
I'm trying to perform the acquisition of data from the internet on the load
I am trying to perform a JNDI-lookup from within the GWT devmode. I have
I'm trying to perform a select query over two tables, one containing, for example,
I'm trying to perform encryption and decryption (Rijndael 256, ecb mode) in two different
I am trying to perform two different SUMs within a single query. user points
I am trying to setup Dozer to perform a complex mapping between my two
I am trying to perform a SQL query that acts in two parts. First,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.