I am trying to sessionStorage a display state (block/none) of a div filter-panel--content
when my site refreshes. I have multiple div's with that class. Inside that div are some checkboxes that refreshes the site after they change state.
That works perfectly, but when my site refreshes this div stays closed 'display:none'. But I try to sessionStorage the display state.
This is a cutting of my sidebar:
<div class="filter-panel filter--property facet--property" data-filter-type="value-list" data-field-name="f">
<div class="filter-panel--flyout" style="border:none;">
<div id="label-it" class="label-it"><label class="filter-panel--title"><h3 class="rums">LED</h3><div id="klapp" class="klapp"></div></label></div>
<div class="filter-panel--content">
[...]
</div>
<div class="filter-panel filter--property facet--property" data-filter-type="value-list" data-field-name="f">
<div class="filter-panel--flyout" style="border:none;">
<div id="label-it" class="label-it"><label class="filter-panel--title"><h3 class="rums">Housing style</h3><div id="klapp" class="klapp"></div></label></div>
<div class="filter-panel--content">
[...]
</div>
When the div "label-it" is clicked, the div "filter-panel--content" toggles display:none
and display:block
, inside "filter-panel--content" are some checkboxes that refreshes the site.
And at this point I want to store the display property of "filter-panel--content"
This is my first solution of getting the sessionStorage
:
sessionStorage.setItem("filterstate", $(this).closest(".filter-panel--flyout").find(".filter-panel--content div").css("display"));
This is my first solution of setting the sessionStorage
, it's in the toggle function:
$('.label-it').click(function() {
$(this).find(".filter-panel--title div").toggleClass('klapp klappe');
$(this).closest(".filter-panel--flyout").find(".filter-panel--content div").toggle(350)
sessionStorage.setItem("filterstate", $(this).closest(".filter-panel--flyout").find(".filter-panel--content div").css("display"));
});
I need some help :)
2nd Try:
I tried to implement localStorage
unfortunately it doesn't work.. I rly need some help..
SET:
$('.label-it').click(function() {
$(this).find(".filter-panel--title div").toggleClass('klapp klappe');
$(this).closest(".filter-panel--flyout").find(".filter-panel--content div").toggle(350)
var panel = $(this).closest(".filter-panel--flyout").find(".filter-panel--content div");
if ( panel.style.display === "block") {
localStorage.setItem("isOpen", true);
} else {
localStorage.setItem("isOpen", false);
}
});
GET:
$(document).ready(function() {
var isOpen = localStorage.getItem("isOpen");
if (isOpen === "true") {
$(this).closest(".filter-panel--flyout").find(".filter-panel--content div").css('display', 'block');
} else {
$(this).closest(".filter-panel--flyout").find(".filter-panel--content div").css('display', 'none');
}
});