How can I keep an accordion collapse or open inside a nav-tab after page refresh using Bootstrap 4 and Jquery?

How can I keep an accordion collapse or open inside a nav-tab after page refresh using Bootstrap 4 and Jquery?

I'm trying to mix Bootstrap 4 Nav-tab and Accordion. My nav-tab is working properly after page refresh it stays on the active tab and I want to do the same with the accordion. I want the keep the active accordion open or collapse when the page was refresh.

  1. <ul class = "nav nav-tabs">
  2. <li class = "nav-item"><a href = "#monitor" data-toggle = "tab" class = "nav-link active">Book Monitoring</a></li>
  3. <li class = "nav-item"><a href = "#history" data-toggle = "tab"class = "nav-link">Book's History</a></li>
  4. </ul>
  5. <!-- STARTS OF NAV-TAB -->
  6. <div class = "tab-content">
  7. <div class = "tab-pane container active" id = "monitor">
  8. <br />
  9. <h5>Monitoring of Books </h5>
  10. </div>
  11. <div class = "tab-pane container" id = "history">
  12. <br />
  13. <h5>History of Borrowed Books</h5>
  14. <!-- STARTS OF ACCORDION -->
  15. <div id = "accordion">
  16. <div class = "card">
  17. <div class = "card-header">
  18. <a href = "#stud_collapse" class = "card-link" data-toggle = "collapse">Borrowed by Students</a>
  19. </div>
  20. <div class = "collapse" id = "stud_collapse" data-parent = "#accordion">
  21. <div class = "class-body">
  22. Student's Data  (Form and Tables Here)
  23. </div>
  24. </div>
  25. </div>
  26. <div class = "card">
  27. <div class = "card-header">
  28. <a href = "#teach_collapse" class = "card-link" data-toggle = "collapse">Borrowed by Teachers</a>
  29. </div>
  30. <div class = "collapse" id = "teach_collapse" data-parent = "#accordion">
  31. <div class = "class-body">
  32. Teacher's Data (Form and Tables Here)
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>

  39. <script>
  40. $(function() {
  41. //This is working properly for the Bootstrap Nav-Tab
  42. $('a[data-toggle="tab"]').click(function (e) {
  43. e.preventDefault();
  44. $(this).tab('show');
  45. });

  46. $('a[data-toggle="tab"]').on("shown.bs.tab", function (e) {
  47. var id = $(e.target).attr("href");
  48. localStorage.setItem('selectedTab', id)
  49. });

  50. var selectedTab = localStorage.getItem('selectedTab');
  51. if (selectedTab != null) {
  52. $('a[data-toggle="tab"][href="' + selectedTab + '"]').tab('show');
  53. }
  54. //This is what Ive tried but NOT working for the Bootstrap Accordion (collapsible), I tried to edit the code above for the accordion but no luck:

  55. $('a[data-toggle="collapse"]').click(function (e) {
  56. e.preventDefault();
  57. $(this).collapse('show');
  58. });

  59. $('a[data-toggle="collapse"]').on("shown.bs.collapse", function (e) {
  60. var id = $(e.target).attr("href");
  61. localStorage.setItem('selectedAccordion', id)
  62. });

  63. var selectedAccordion = localStorage.getItem('selectedAccordion');
  64. if (selectedAccordion != null) {
  65. $('a[data-toggle="collapse"][href="' + selectedAccordion + '"]').collapse('show');
  66. }
  67. });
  68. </script>