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

  • SEARCH
  • Home
  • 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 278637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:16:15+00:00 2026-05-12T01:16:15+00:00

I will try to keep this short. I’m trying to create boxes of text

  • 0

I will try to keep this short. I’m trying to create boxes of text that hide or show when user clicks on a expand button. I’m using the toggle() method.

The markup is like this:

<span id="toggle0">+</span>
<div id="toggle0Container">
    blablabla...
<div>

<span id="toggle1">+</span>
<div id="toggle1Container">
    blablabla...
<div>

<span id="toggle2">+</span>
<div id="toggle2Container">
    blablabla...
<div>

// etc

The #toggle0 is supposed to toggle the #toggle0Container, the #toggle1 toggles the #toggle1Container and so on. This is all generated by PHP so there can be any number of these containers.

Here’s the JQuery code:

$(document).ready(function() {
    // array of numbers, it's pretty redundant, there will never be more than 30 containers
    var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36];
    // hide all containers first
    jQuery.each(arr, function() {
        $('#toggle' + this + 'Container').hide();
    });
    // now the toggle buttons
    jQuery.each(arr, function() {
        $('#toggle' + this).click(function() {
            // this line is not working
            $('#toggle' + this + 'Container').slideToggle('slow');
            // and this just changes the toggle button from + to -
            if ($(this).html() == '+') {
                $(this).html('-');
            } else {
                $(this).html('+');
            }
        });
    });
});

It works except for the toggling part (I have added comment above the line that’s not working). Where’s the problem?

  • 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-05-12T01:16:16+00:00Added an answer on May 12, 2026 at 1:16 am

    I would follow mgroves’s approach but if you really want to do the array thing, or just want to figure out how to use each() properly, here’s how you should use it:

    $(document).ready(function() {
        // array of numbers, it's pretty redundant, there will never be more than 30 containers
        var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36];
        // hide all containers first
        jQuery.each(arr, function(index, item) {
            $('#toggle' + item + 'Container').hide();
        });
        // now the toggle buttons
        jQuery.each(arr, function(index, item) {
            $('#toggle' + item).click(function() {
                // this line is not working
                $('#toggle' + item + 'Container').slideToggle('slow');
                // and this just changes the toggle button from + to -
                if ($(this).html() == '+') {
                    $(this).html('-');
                } else {
                    $(this).html('+');
                }
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will try and keep this short and to the point. Given the following
I will try to keep this as short and specific as I can. This
Hello there I will try and keep this simple and short I have a
I will try to keep this as simple as possible. I have a rather
I have a complicated question, so I will try and keep it as short
ok this is a head wrecker and I will try to keep it brief.
I will try and word this question the best I can, for it is
I will try and illustrate my question with examples, I am attempting to create
I will try to make this as clear as I can, but if you
I have the need to create a plugin system that will have dependency support

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.