Bootstrap - Keeping track of the collapse state of <div> elements after page refresh

Looking for some guidance as a javascript/jquery beginner - I am utilizing Bootstrap along with data-toggle and collapse classes to display or hide divs. I have been searching online trying to find a solution that will maintain the state of all divs, whether they are identified by a unique ID or a CLASS, even after page refreshes. I've come across discussions regarding cookies and local storage, but I am open to using either method (although I encountered errors with $.cookie is not a function, so maybe local storage is preferable?).

The problem is that most examples focus on maintaining accordion states, which may not directly apply in my case. I have attempted to tweak various code snippets but have not been successful.

Here is an excerpt from my code:

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<div class="panel panel-danger">
  <div data-toggle="collapse" href="#duesoon" style="cursor: pointer;" class="panel-heading">
    <font class="panel-title"><span class='glyphicon glyphicon-fire'></span> Top 5 Expiring Tasks</font>
  </div>
  <div id="duesoon" class="collapse">
    <table class="table table-hover table-striped table-condensed">
      <thead>
        <tr>
          <th class='col-md-7'>Name</th>
          <th class='col-md-5'>End Date</th>
        </tr>
      </thead>
      <tbody>
        <tr style="cursor: pointer;" onclick="document.location = '?action=view&type=project&id=2">
          <td><span class='glyphicon glyphicon-triangle-right'></span> Take Out The Trash</td>
          <td>Yesterday</td>
        </tr>
      </tbody>
    </table>
  </div>
  <div data-toggle="collapse" href="#urgency" style="cursor: pointer;" class="panel-heading">
    <font class="panel-title"><span class='glyphicon glyphicon-fire'></span> Top 5 Urgent Tasks</font>
  </div>
  <div id="urgency" class="collapse">
    <table class="table table-hover table-striped table-condensed">
      <thead>
        <tr>
          <th class='col-md-7'>Name</th>
          <th class='col-md-5'>Priority</th>
        </tr>
      </thead>
      <tbody>
        <tr style="cursor: pointer;" onclick="document.location = '?action=view&type=project&id=1">
          <td><span class='glyphicon glyphicon-triangle-right'></span> Save the Whales</td>
          <td>Critical</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

As seen in the code snippet, there's a link or button triggering the show/hide functionality of a div.

View this on JSFiddle too: http://jsfiddle.net/w8psgvaa/2/

I came across this piece of code;

 $('.collapse').on('hidden', function() {
       // store this.id
 }).on('shown', function() {
       // delete this.id
 });

 $(document).ready(function(){
     $(".collapse").collapse().each(function(){
         if( isStored( this.id ) ) {
             $( this ).collapse( 'hide' );
         }
     });
 });​

However, it seems incomplete, and some divs start off collapsed (as evident in my example). Any assistance would be greatly appreciated!

Answer №1

You are on the right track with your approach. Here is my proposed solution:

Utilize the LocalStorage feature found in modern web browsers.

  • When a division is collapsed: remove the ID of the division from local storage.
  • When a division is expanded: add the ID of the division to local storage.

Implementing this is quite straightforward:

var shown = []

// On collapse
shown.remove($(this).attr('id'));
localStorage.setItem('shown', shown);

// On expand
shown.push($(this).attr('id'));
localStorage.setItem('shown', shown);

// On page load
var shown = localStorage.getItem('shown');
for (var s in shown) {
    $('#' + s).show(); // Or use appropriate method for expanding the division
}

For further details, refer to:

Answer №2

Check out this practical example that utilizes Bootstrap panel-collapse and LocalStorage, inspired by Hidde's solution. My implementation involves using JSON "stringify" and "parse" methods to save my ids in localStorage as strings. I've also incorporated Bootstrap's collapse events into the code.

// When the document is loaded

var shownOnRefresh = [];
localStorage.setItem('shownOnRefresh', JSON.stringify(shownOnRefresh));

$('#myParentElement').on('shown.bs.collapse', '.panel-collapse', function() {
        shownOnRefresh = JSON.parse(localStorage.getItem('shownOnRefresh'));
        if ($.inArray($(this).attr('id'), shownOnRefresh) == -1) {
            shownOnRefresh.push($(this).attr('id'));
        };
        localStorage.setItem('shownOnRefresh', JSON.stringify(shownOnRefresh));
});

$('#myParentElement').on('hidden.bs.collapse', '.panel-collapse', function() {
        shownOnRefresh = JSON.parse(localStorage.getItem('shownOnRefresh'));
        shownOnRefresh.splice( $.inArray($(this).attr('id'), shownOnRefresh), 1 );//remove item from array
        localStorage.setItem('shownOnRefresh', JSON.stringify(shownOnRefresh));
});

// On page refresh
var shownOnRefresh = JSON.parse(localStorage.getItem('shownOnRefresh'));
for (var i in shownOnRefresh ) {
    $('#' + shownOnRefresh [i]).addClass('in');
}

Although I'm new to jQuery, this script gets the job done. Feel free to optimize it further!

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

Centered CSS Box with Pointing Arrow

I'm looking for a way to create a unique div design that includes an arrow pointing downwards attached to the bottom. After some exploration, I was able to achieve this look through this process: http://jsfiddle.net/hyH48/. However, my challenge lies ...

npm will only update when modifications are made to index.js

Recently diving into the world of React, I've encountered an issue with updating my website when making changes to files within my project. While modifications to the index.js file reflect on the site, any adjustments made to imported apps do not get ...

What would be the ideal labels for the parameters within Array.reduce?

When it comes to enhancing code readability, what naming convention should be employed when naming callback arguments in Array.reduce for optimal best practices? const studentAges= [15,16,14,15,14,20] Generalized Approach const sum = studentAges.reduce ...

Is it detrimental to have an excessive amount of simultaneous AJAX connections?

In the process of developing a JavaScript based application of significant size, I frequently encounter situations where up to eight (8) AJAX requests are initiated concurrently. In older browsers such as IE6, this can result in certain requests being term ...

Why aren't the media breakpoints in my SASS project being recognized?

For a React project, I initially used CSS but switched to SASS for incorporating a "dark mode" feature. To organize my files according to the documentation, I also utilize bootstrap. /* App.scss */ @import "~bootstrap/scss/bootstrap.scss"; @imp ...

Tips on incorporating several class names into Next.js elements

My current challenge involves an unordered list element with the following structure: <ul className={styles["projects-pd-subdetails-list"]}> {detail.subdetails.map((sub) => ( <li className={styles[ ...

Using various Content-Types within a single path with Serverless Next JS

Is it possible to achieve the following scenario using serverless on Vercel with Next JS? I have a route /product/[id].tsx. When a request is sent with the header Accept: text/html, I want it to follow the normal Next JS flow and display a React page. How ...

Adjust the CSS styling for a newly added row in a JavaFX TableView

Apologies for any errors, I am French. I am currently facing an issue with an empty tableView in my project. There is a button labeled "Add" that adds a row to the tableView when clicked. Another button labeled "Cancel" appears when a row in the tableView ...

The background image stubbornly refuses to stretch to the full width of the section container

Looking at my code example and the screenshot provided, it's clear that the image is not fitting into the container even when set to 100% width or utilizing other properties. Below you will see the code snippet: .about-us{ background-image: url(i ...

A fun jQuery game where you match numbers together! Each selection you make is influenced by the one before it

Check out this cool jQuery code! When you click on an item in the first list, it turns green. If you then click on the same item in the second list, it will appear red. $(“#list1).on(“click”,”li”, function(){ var nomber = $(this); $(“#list2”) ...

How can I make Vuejs update v-model when the text input is changed using Jquery?

I am currently working with 4 inputs that have a timeselector popup. The jquery timepicker plugin I am using does not seem to provide an obvious way to manipulate an input through a callback function. Interestingly, Vuejs does not detect changes made to th ...

Exploring the potential of Bootstrap/bootflat and jQuery progress bars

I am attempting to display and animate a progress bar while loading an HTML page into a div using jQuery. The progress bar I am using is from Bootstrap (Bootflat) and has the following structure: <div class="progress" style="visibility:hidden;"> ...

Incorporating Blank Class into HTML Tag with Modernizr

Currently, I am experimenting with Modernizr for the first time and facing some challenges in adding a class to the HTML tag as per the documentation. To check compatibility for the CSS Object Fit property, I used Modernizr's build feature to create ...

Encountering issues with updating state object in setState function

Review the code snippet below: {split.participants.map(friend => { return <div key={Math.random()} className="form-check my-2 d-flex align-items-center justify-content-between"> <div ...

Making a POST request to a Next.js API route results in a 500 Internal Server Error being sent back

Check out the code in createComment.ts file, which serves as a Next.js api route: import type { NextApiRequest, NextApiResponse } from 'next' import sanityClient from "@sanity/client" const config = { dataset: process.env.NEXT_PUBLI ...

Is it possible to use jQuery validate for remote parsing with two fields in a single call

Currently, I am facing an issue while trying to parse two values using jQuery's validate plugin to compare with an SQL database. The DateReceived value is successfully parsed, but the CentreID value always appears as null. Below is the code snippet I ...

Elevate the placeholder in Angular Material 2 to enhance its height

I want to make material 2 input control larger by adjusting the height property of the input using CSS <input mdInput placeholder="Favorite food" class="search-grid-input"> .search-grid-input { height:30px!important; } As a result, the image o ...

How can I receive the response from a GET request using React Query? I am currently only able to get the response

I have created a search component where I input a name in the request, receive a response, and display it immediately. However, after the first input submit, I get undefined in response. Only after the second submit do I get the desired response. The tec ...

Conceal elements with a single click of a button

How can I use jQuery to hide all li elements with an aria-label containing the word COMPANY when the Search from documents button is clicked? Here is the HTML code: <ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content" id="ui-id-1" t ...

How can I retrieve a password entered in a Material UI Textfield?

My code is functioning properly, but I would like to enhance it by adding an option for users to view the password they are typing. Is there a way to implement this feature? const [email, setEmail] = useState(''); const [password, setPassword] = ...