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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:20:30+00:00 2026-05-20T11:20:30+00:00

I want to toggle a div element (expand/collapse) when clicked. I have many div

  • 0

I want to toggle a div element (expand/collapse) when clicked.
I have many div elements, on click to new element, I want to collapse the previous one and expand the current clicked one.

I tried using static type variable to save the instance of previous div tag and compared with the current selection, but I don’t know why is it not working.

Searching about this, I got similar code idea to collapse all div and then expand the current selected only, but I want to just toggle the previous one with new one, not collapse all div and expand the selected (though I would be using it if other way is not possible)

Can it be done using static variables of js?

  • 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-20T11:20:31+00:00Added an answer on May 20, 2026 at 11:20 am

    At its simplest, you can simply do something like this:

    var divs = document.getElementsByTagName('div'),
        collapseClass = 'collapsed',
        current = divs[0];
    
    // Hide all elements except first, add click event hander
    for(var i = 0, len = divs.length; i < len; i++){
        divs[i].onclick = function(){
            if(this !== current){
                toggle(this, current);
                current = this;
            }
        };
    
        if(i > 0) toggle(divs[i]);
    }
    

    This will store the current element in a variable, then toggle it when another element is clicked. It also uses an if statement to check if the currently clicked element is the one currently visible element, and only toggles if its not.

    See a working demo of this here: http://jsfiddle.net/GaxvM/

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

Sidebar

Related Questions

I have a basic toggle switch that shows the clicked on div while closing
I have a small progressive disclosure (text expand and collapse) but the toggle text
I have elements that are under an element with opacity:0.5 that I want to
I have a ListBox control that I want to change into having a toggle
I have done a hide and show of a div element, on clicking a
I have a complicated jQuery form and I want to disable several form elements
I have about 25 show/hide (toggle) elements on a page like so... HTML: <h2><a
I have this code to show/hide (toggle) a div when clicking a link: $('#complete-services-link').toggle(
i have a panel having slide toggle effect on click of a button along
I have found this code (jQuery): $('.toggle').click(function() { $('.container').eq($(this).index()).toggle('fast'); }); This is my HTML:

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.