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 697027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:07:32+00:00 2026-05-14T03:07:32+00:00

I have a question about some functionality I’m trying to add to my jQuery

  • 0

I have a question about some functionality I’m trying to add to my jQuery to enable a button or text to expand/contract all the divs on click… and I’d like to figure out how to keep the first div open when the page loads.

Here is the jQuery:

(document).ready(function(){

//Hides containers on load
$(".toggle_container").hide(); 

//Switch "Open" and "Close" state on click
$("h2.trigger").toggle(function(){
    $(this).addClass("active");
    }, function () {
    $(this).removeClass("active");
});

//Slide up and down on click
$("h2.trigger").click(function(){
    $(this).next(".toggle_container").slideToggle("slow");
    });
});

And the css:

// uses a background image with an on (+) and off (-) state stacked on top of each other
h2.trigger { background: url(buttonBG.gif) no-repeat;height: 46px;line-height: 46px;width: 300px;font-size: 2em;font-weight: normal;}
h2.trigger a {color: #fff;text-decoration: none; display: block;}
h2.active {background-position: left bottom;}
.toggle_container { overflow: hidden; }
.toggle_container .block {padding: 20px;}

And the html

<h2 class="trigger"><a href="#">Heading</a></h2> 
<div class="toggle_container"> 
  <div class="block">Stuff goes here</div> 
</div>

<h2 class="trigger"><a href="#">Heading 2</a></h2> 
<div class="toggle_container"> 
  <div class="block">Stuff goes here</div> 
</div>

So it works great and looks great. However, when I try to get it to keep the first instance open, the background image that should adjust show the (-) state doesn’t change. The code I used to this was:

$(".toggle_container:first").show();

So, my question is, does anyone know of an easier way to show the first instance of this as open without having to created specials rules/class for the first item? Also, any ideas about how to make an open all/close all link?

Thanks!

  • 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-14T03:07:33+00:00Added an answer on May 14, 2026 at 3:07 am

    You can change all your jQuery to this:

    $(".toggle_container:not(:first)").hide(); 
    $("h2.trigger:first").addClass("active");
    
    $("h2.trigger").click(function(){
      $(this).toggleClass("active");
      $(this).next(".toggle_container").slideToggle("slow");
    });
    

    This hides all but the first .toggle_container, applies the class="active" to it’s matching <h2>, and the toggle() is just simpler, a click now toggles the class…so we can just add active to match it being open on page load and it’ll behave correctly.

    Also, a open/close all would look like this:

    <a href="#" id="openCloseAll">Open All</a>
    

    jQuery like this:

    $("#openCloseAll").toggle(function() {
      $(this).text("Close All");
      $(".toggle_container:hidden").prev(".trigger").click();
    }, function() {
      $(this).text("Open All");
      $(".toggle_container:visible").prev(".trigger").click();
    });
    

    This opens all closed on the first click, changes it’s text to accurately describe what the next click will do, and closes all open on the next click, then switches back, rinse, repeat.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, i have some question about xml Documents in Java. Can i get all
I have a question about reflection I am trying to have some kind of
I'm working on some JQuery code and I have a question about caching selectors.
I have some question about WH_MOUSE. From what I've read, by putting the hook
I have a question about laying out some swing components. Say, I have a
i have what i hope is a quick question about some code i am
I have a question about SqlDataReader: Is there some way to modificated the values
I have a question about python mechanize's proxy support. I'm making some web client
I'm currently developing some things in Python and I have a question about variables
In continuation of the question at this link I wanted to add some functionality

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.