user10243016
user10243016

Reputation:

Button Toggle Bootstrap Keep State After Refresh

I have three toggle buttons and I want to keep toggle state after refresh page.I read many things but dont know how to use them in my case.

This is buttons and divs

    <button class="badge badge-danger mb-4 d-flex left" type="button" data-toggle="collapse" data-target="#personal-data" aria-expanded="false" aria-controls="personal-data"></button>
    <button class="badge badge-danger mb-4 mr-md-4 ml-md-4" type="button" data-toggle="collapse" data-target="#email-change" aria-expanded="false" aria-controls="email-change"></button>
    <button class="badge badge-danger mb-4" type="button" data-toggle="collapse" data-target="#user-history" aria-expanded="false" aria-controls="user-history"></button>
    <div class="collapse" id="personal-data">
                <div class="form-group col-12">

     <div class="col-12">
            <div class="collapse" id="email-change">

     <div class="col-12">
            <div class="collapse" id="user-history">

Upvotes: 2

Views: 3163

Answers (2)

Dave
Dave

Reputation: 599

I tweaked Object's answer to remove the key from localstorage if hidden. From what I could find, there isn't a need to check if the key exists before attempting to remove. This reduces the amount of items being kept in localstorage.

$(document).ready(function () {
    //If shown.bs.collapse add the unique id to local storage
    $(".collapse").on("shown.bs.collapse", function () {
        localStorage.setItem("coll_" + this.id, true);
    });
    //If hidden.bs.collaspe remove the unique id from local storage
    $(".collapse").on("hidden.bs.collapse", function () {
        localStorage.removeItem("coll_" + this.id);
    });
    //If the key exists and is set to true, show the collapsed, otherwise hide
    $(".collapse").each(function () {
        if (localStorage.getItem("coll_" + this.id) == "true") {
            $(this).collapse("show");
        }
        else {
            $(this).collapse("hide");
        }
    });
});

Upvotes: 2

Kashyap Merai
Kashyap Merai

Reputation: 862

Use can use below way to keep state after a refresh.

  • Cookie
  • LocalStorage

Both of these can store client-side data which will preserve your state.

Provide each collapse element a unique id across the website so there'll be no room for conflict. So based on user action collapse or un-collapses an element you'll update state in storage. Now using bootstrap event and API, you can collapse or un-collapses an element based on storage state.

Here is a working demo: https://jsfiddle.net/c1ovt5g4/

<button class="badge badge-danger left" type="button" data-toggle="collapse" data-target="#personal-data" aria-expanded="false" aria-controls="personal-data">Personal Data</button>
<button class="badge badge-danger " type="button" data-toggle="collapse" data-target="#email-change" aria-expanded="false" aria-controls="email-change">Email Change</button>
<button class="badge badge-danger" type="button" data-toggle="collapse" data-target="#user-history" aria-expanded="false" aria-controls="user-history">User History</button>
<div class="collapse" id="personal-data"> PERSONAL DATA</div>
<div class="collapse" id="email-change"> EMAIL CHANGE</div>
<div class="collapse" id="user-history">USER HISTORY</div>
$(".collapse").on("hidden.bs.collapse", function() {
  localStorage.setItem("coll_" + this.id, false);
});

$(".collapse").on("shown.bs.collapse", function() {
  localStorage.setItem("coll_" + this.id, true);
});

$(".collapse").each(function() {
  if (localStorage.getItem("coll_" + this.id) == "true") {
    $(this).collapse("show");
  }
});

Upvotes: 6

Related Questions