What is the best way to conceal panels while a bootstrap is being utilized?

I have been working on a bootstrap panel that consists of three panels: Panel 1, Panel 2, and Panel 3. The functionality of this panel is working correctly under normal circumstances.

My Approach:

  • Clicking on Panel 1 should display its content while hiding both Panel 2 and Panel 3.
  • Similarly, clicking on Panel 2 should show its content while hiding Panel 1 and Panel 3.
  • Finally, clicking on Panel 3 should reveal its content while concealing both Panel 1 and Panel 2.

https://i.sstatic.net/STXw3.png

However, I also want to optimize the space usage. This means that the active panel should always appear first so that the layout remains organized (All active panels must be positioned at the top).

This is the code snippet I am currently using:

HTML

<div class="container">
<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
          Panel #1<span class="glyphicon glyphicon-chevron-up"></span>
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in">
      <div class="panel-body">
      panel 1 content
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
          Panel #2 <span class="glyphicon glyphicon-chevron-up"></span>
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse">
      <div class="panel-body">
        panel 2 content
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
          Panel #3<span class="glyphicon glyphicon-chevron-down"></span>
        </a>
      </h4>
    </div>
    <div id="collapseThree" class="panel-collapse collapse">
      <div class="panel-body">
        panel 3 content
      </div>
    </div>
  </div>
</div>
</div>

Script :

$('.collapse').on('shown.bs.collapse', function(){
$(this).parent().find(".glyphicon-chevron-down").removeClass("glyphicon glyphicon-chevron-down").addClass("glyphicon glyphicon-chevron-up");
}).on('hidden.bs.collapse', function(){
$(this).parent().find(".glyphicon-chevron-up").removeClass("glyphicon glyphicon-chevron-up").addClass("glyphicon glyphicon-chevron-down");
});

Does anyone have any suggestions on how I can achieve this with Bootstrap's collapse panel?

Answer №1

Ensure that the active panel is positioned at the first position.

To achieve this using jQuery, you can use the following solution:

$('#accordion').prepend($(this).closest('div.panel-default'));

When moving the active tab to the first position, you can save its current position so that when it is clicked again, all panels are displayed and the active one is placed back in the correct place:

$(this).closest('div.panel-default')
       .insertAfter($('div.container div.panel-default').eq(idx))

The complete snippet for this functionality:

$('.collapse').on('show.bs.collapse', function () {
  $(this).parent().find(".glyphicon-chevron-down").removeClass("glyphicon glyphicon-chevron-down").addClass("glyphicon glyphicon-chevron-up");
  $(this).closest('div.panel-default').attr('idx', $(this).closest('div.panel-default').index());
  $('#accordion').prepend($(this).closest('div.panel-default'));
  $('div.container div.panel-default').not($(this).closest('div.panel-default')).hide();
}).on('hide.bs.collapse', function () {
  $(this).parent().find(".glyphicon-chevron-up").removeClass("glyphicon glyphicon-chevron-up").addClass("glyphicon glyphicon-chevron-down");
});

$('#accordion a.accordion-toggle').on('click', function (e) {
  if ($('div.container div.panel-default:visible').not($(this).closest('div.panel-default')).length == 0) {
    var idx = $(this).closest('div.panel-default').attr('idx');
    $('div.container div.panel-default').show();
    $(this).closest('div.panel-default').insertAfter($('div.container div.panel-default').eq(idx))
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<div class="container">
    <div class="panel-group" id="accordion">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                        Panel #1<span class="glyphicon glyphicon-chevron-up"></span>
                    </a>
                </h4>
            </div>
            <div id="collapseOne" class="panel-collapse collapse in">
                <div class="panel-body">
                    panel 1 content
                </div>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                        Panel #2 <span class="glyphicon glyphicon-chevron-down"></span>
                    </a>
                </h4>
            </div>
            <div id="collapseTwo" class="panel-collapse collapse">
                <div class="panel-body">
                    panel 2 content
                </div>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
                        Panel #3<span class="glyphicon glyphicon-chevron-down"></span>
                    </a>
                </h4>
            </div>
            <div id="collapseThree" class="panel-collapse collapse">
                <div class="panel-body">
                    panel 3 content
                </div>
            </div>
        </div>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble getting my Ajax/jquery script to function properly in Codeigniter

Do you know why this code works without jquery but doesn't update with ajax? I have to refresh the page to see the updates. Any help in identifying the issue would be appreciated. Here is the jQuery code being used: $(document).ready(function(){ //g ...

Entering the ajax success handler

I have created this code snippet and I'm looking for a way to pass the exact 'this' value (since there are multiple items with this class) into the ajax success function. Any suggestions on how to achieve that? $(document).on('click&ap ...

Selecting a new image to use as the background on a website by utilizing

While exploring JavaScript, I successfully altered the background color: <script> function changeColor(id,color) { id.style.backgroundColor=color; } </script> <div id="container"> <p> Select a Color to ...

Using Python with Selenium to extract information through hover actions

driver.get(link) time.sleep(2) lol = driver.find_element_by_xpath("//*[@id='table_div']/div/div[1]/table/tbody/tr[2]/td[10]/div/div") hover = ActionChains(driver).move_to_element(lol) hover.perform() I have a code snippet here where I am attempt ...

Troubleshoot the Error: EEXIST - Directory already exists at 'C:UsersPhantom' while setting up a React application[RESOLVE]

I'm trying to set up react and start my first project, but I encountered an issue during the installation process. How can I resolve this? Error: EEXIST: file already exists, mkdir 'C:\Users\Phantom' TypeError: Cannot read propert ...

Ensuring the value of a v-text-field in Vuetify using Cypress

I am currently developing an end-to-end test suite using Cypress for my Vue and Vuetify frontend framework. My goal is to evaluate the value of a read-only v-text-field, which displays a computed property based on user input. The implementation of my v-tex ...

Mutating observable values in Mobx without using action functions

I encountered the following issue: proxyConsole.js:54 Error: [mobx] Invariant failed: Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried t ...

Node application sends requests successfully, whereas in JS (Vue), the request is not functioning properly

Greetings! I have a backend that receives a request with an image for storage. When testing it with Postman and the code snippet below, everything works perfectly: var axios = require('axios') var FormData = require('form-data') var fs ...

Transforming a dynamic background image into dynamic HTML elements

Having trouble parsing a background image to HTML elements. The images are retrieved from a database and the HTML elements are dynamically created. However, the image is not displaying. I attempted to include JavaScript in the while loop but encountered ...

Why is jQuery not recognizing the variable, but when I manually input the exact same value it works?

Explained using a code snippet: $(".myParent .myChild:nth-child(3n)").css('border-top-color','#ffffff'); √ Success myVar = "3n"; $(".myParent .myChild:nth-child(myVar)").css('border-top-color','#ffffff'); ...

Tips for sending substantial amounts of text (including single quotes) to an Angular Modal

There is a button present on the page <button class="btn btn-info btn-small-round" ng-show="row.Notes.length > 0" ng-click="showNotes({{row.Notes}})">View Notes</button> However, I am encountering a few issues. Firstly, the modal fails to ...

Issue with File Existence Resulting in ENOENT Error

Check out this gist for more information: https://gist.github.com/973e70bde8e6a530c489 I have encountered an interesting problem with two scenarios. In one scenario, I can parse a CSV file that is already on the box without any issues. However, in the oth ...

Illuminate objects using three.js

My expertise in shaders and three.js is limited, however I am currently experimenting with creating a dynamic glowing effect similar to lights being flicked on and off. Currently, I am adjusting the color saturation and lightness which somewhat achieves ...

Guide on aggregating values of a specific property within an array of objects, considering a given condition

As a junior Web Developer seeking some guidance to solve a problem, I am reaching out here for the first time. Please bear with me if I miss any crucial details. The data array I have looks like this: [ {x: Date(1234), y: 0} {x: Date(1235), y: 0} {x: ...

Guide to modifying the value of a JSON attribute in JavaScript

Here's a snippet from my json file: { "@id": "2", "@samsid": "d7058536c89b46c0b58117eff64372f7", "@productname": "Another paid for", "@downloaddate": "2013-05-28 11:37:12Z", "@downloadlocation": "2AFoGZVJpFHzspQD9UoE2C4VFYc8idiO4ebaRx4uEvtG+ ...

Obtaining a JavaScript proxy from a WCF service using webHttpBinding

I have configured all the necessary endpoints, bindings, and behaviors to consume a service using JSON. However, I am struggling to find a way to generate a JavaScript proxy for accessing the service from my client-side JavaScript with jQuery through Ajax. ...

Exploring Vue.prototype attributes and utilizing them

I'm facing a challenge while attempting to globally set a property using Vue.prototype. This is what I currently have: microsoftTeams.initialize(); microsoftTeams.getContext((context) => { Vue.prototype.$teamsContext = true; }); new Vue({ ...

Improving the Speed of ASP.NET TreeView

How can we optimize performance when using the TreeView component? When I say optimize performance, I am referring to reducing the number of client-server trips, such as postbacks. Does this imply that the majority of the business logic will need to be i ...

Tips for transferring data to an entry component in Angular 2 using ng-bootstrap modal

I've been grappling with sending data to a custom modal content component in Angular 2. My objective is to have the flexibility of calling this modal from any other component without duplicating code. Despite my efforts, including referencing the "Com ...

Sending data to functions

Hello all, I'm a beginner with Vue so please bear with me :) I am facing a challenge where I have a function that retrieves user attributes and based on those attributes, I need to run a GraphQL query in another function. Both functions are under the ...