Checkbox filter products with jquery and keep state with reload











up vote
0
down vote

favorite












I want to create a page where I can filter products with checkboxes but also to keep the filter choices on page reload.



<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="1" value="option1" >
<label for="1">Adidas</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="2" value="option2" >
<label for="1">Nike</label>
</div>

<script>
var checkboxValues = JSON.parse(localStorage.getItem('checkboxValues')) || {},
$checkboxes = $("#checkbox-container :checkbox");

$checkboxes.on("change", function(){
$checkboxes.each(function(){
checkboxValues[this.id] = this.checked;
});

localStorage.setItem("checkboxValues", JSON.stringify(checkboxValues));
});

// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});

$('input[type="checkbox"]').change(function() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.products >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
$('.products >div').show();

}
});
</script>


I have found both jquery scripts and they both work fine by themselves. By this I mean that I can get to filter the products when checking/unchecking the checkboxes. When checking a checkbox and reloading the page, the checkbox is correctly checked but products has not been filtered. So I need to merge the 2 scripts so that products also will be filtered when reloading the page with a checked checkbox.



Hope someone can guide me a bit here.



--- UPDATE ---



I have created an example here: maxie.dk/filter.php



1) Tick a checkbox



2) Product filter is activated and only products with matching value is displayed



3) Refresh page



4) Checkbox checked is kept as it should but all products are now again visible. Only matching products to the ticked checkbox should be visible.



Thanks










share|improve this question
























  • More explanation is needed here.
    – Mark Adesina Omoniyi
    Nov 22 at 13:37










  • I am sorry for not explaining thorough enough. I have created an example here: maxie.dk/filter.php 1) Tick a checkbox 2) Product filter is activated and only products with matching value is displayed 3) Refresh page 4) Checkbox checked is kept as it should but all products are shown. Only matching products should be visible
    – MazeyMazey
    Nov 22 at 15:00















up vote
0
down vote

favorite












I want to create a page where I can filter products with checkboxes but also to keep the filter choices on page reload.



<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="1" value="option1" >
<label for="1">Adidas</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="2" value="option2" >
<label for="1">Nike</label>
</div>

<script>
var checkboxValues = JSON.parse(localStorage.getItem('checkboxValues')) || {},
$checkboxes = $("#checkbox-container :checkbox");

$checkboxes.on("change", function(){
$checkboxes.each(function(){
checkboxValues[this.id] = this.checked;
});

localStorage.setItem("checkboxValues", JSON.stringify(checkboxValues));
});

// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});

$('input[type="checkbox"]').change(function() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.products >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
$('.products >div').show();

}
});
</script>


I have found both jquery scripts and they both work fine by themselves. By this I mean that I can get to filter the products when checking/unchecking the checkboxes. When checking a checkbox and reloading the page, the checkbox is correctly checked but products has not been filtered. So I need to merge the 2 scripts so that products also will be filtered when reloading the page with a checked checkbox.



Hope someone can guide me a bit here.



--- UPDATE ---



I have created an example here: maxie.dk/filter.php



1) Tick a checkbox



2) Product filter is activated and only products with matching value is displayed



3) Refresh page



4) Checkbox checked is kept as it should but all products are now again visible. Only matching products to the ticked checkbox should be visible.



Thanks










share|improve this question
























  • More explanation is needed here.
    – Mark Adesina Omoniyi
    Nov 22 at 13:37










  • I am sorry for not explaining thorough enough. I have created an example here: maxie.dk/filter.php 1) Tick a checkbox 2) Product filter is activated and only products with matching value is displayed 3) Refresh page 4) Checkbox checked is kept as it should but all products are shown. Only matching products should be visible
    – MazeyMazey
    Nov 22 at 15:00













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to create a page where I can filter products with checkboxes but also to keep the filter choices on page reload.



<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="1" value="option1" >
<label for="1">Adidas</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="2" value="option2" >
<label for="1">Nike</label>
</div>

<script>
var checkboxValues = JSON.parse(localStorage.getItem('checkboxValues')) || {},
$checkboxes = $("#checkbox-container :checkbox");

$checkboxes.on("change", function(){
$checkboxes.each(function(){
checkboxValues[this.id] = this.checked;
});

localStorage.setItem("checkboxValues", JSON.stringify(checkboxValues));
});

// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});

$('input[type="checkbox"]').change(function() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.products >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
$('.products >div').show();

}
});
</script>


I have found both jquery scripts and they both work fine by themselves. By this I mean that I can get to filter the products when checking/unchecking the checkboxes. When checking a checkbox and reloading the page, the checkbox is correctly checked but products has not been filtered. So I need to merge the 2 scripts so that products also will be filtered when reloading the page with a checked checkbox.



Hope someone can guide me a bit here.



--- UPDATE ---



I have created an example here: maxie.dk/filter.php



1) Tick a checkbox



2) Product filter is activated and only products with matching value is displayed



3) Refresh page



4) Checkbox checked is kept as it should but all products are now again visible. Only matching products to the ticked checkbox should be visible.



Thanks










share|improve this question















I want to create a page where I can filter products with checkboxes but also to keep the filter choices on page reload.



<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="1" value="option1" >
<label for="1">Adidas</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="2" value="option2" >
<label for="1">Nike</label>
</div>

<script>
var checkboxValues = JSON.parse(localStorage.getItem('checkboxValues')) || {},
$checkboxes = $("#checkbox-container :checkbox");

$checkboxes.on("change", function(){
$checkboxes.each(function(){
checkboxValues[this.id] = this.checked;
});

localStorage.setItem("checkboxValues", JSON.stringify(checkboxValues));
});

// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});

$('input[type="checkbox"]').change(function() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.products >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
$('.products >div').show();

}
});
</script>


I have found both jquery scripts and they both work fine by themselves. By this I mean that I can get to filter the products when checking/unchecking the checkboxes. When checking a checkbox and reloading the page, the checkbox is correctly checked but products has not been filtered. So I need to merge the 2 scripts so that products also will be filtered when reloading the page with a checked checkbox.



Hope someone can guide me a bit here.



--- UPDATE ---



I have created an example here: maxie.dk/filter.php



1) Tick a checkbox



2) Product filter is activated and only products with matching value is displayed



3) Refresh page



4) Checkbox checked is kept as it should but all products are now again visible. Only matching products to the ticked checkbox should be visible.



Thanks







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 15:03

























asked Nov 22 at 13:25









MazeyMazey

608




608












  • More explanation is needed here.
    – Mark Adesina Omoniyi
    Nov 22 at 13:37










  • I am sorry for not explaining thorough enough. I have created an example here: maxie.dk/filter.php 1) Tick a checkbox 2) Product filter is activated and only products with matching value is displayed 3) Refresh page 4) Checkbox checked is kept as it should but all products are shown. Only matching products should be visible
    – MazeyMazey
    Nov 22 at 15:00


















  • More explanation is needed here.
    – Mark Adesina Omoniyi
    Nov 22 at 13:37










  • I am sorry for not explaining thorough enough. I have created an example here: maxie.dk/filter.php 1) Tick a checkbox 2) Product filter is activated and only products with matching value is displayed 3) Refresh page 4) Checkbox checked is kept as it should but all products are shown. Only matching products should be visible
    – MazeyMazey
    Nov 22 at 15:00
















More explanation is needed here.
– Mark Adesina Omoniyi
Nov 22 at 13:37




More explanation is needed here.
– Mark Adesina Omoniyi
Nov 22 at 13:37












I am sorry for not explaining thorough enough. I have created an example here: maxie.dk/filter.php 1) Tick a checkbox 2) Product filter is activated and only products with matching value is displayed 3) Refresh page 4) Checkbox checked is kept as it should but all products are shown. Only matching products should be visible
– MazeyMazey
Nov 22 at 15:00




I am sorry for not explaining thorough enough. I have created an example here: maxie.dk/filter.php 1) Tick a checkbox 2) Product filter is activated and only products with matching value is displayed 3) Refresh page 4) Checkbox checked is kept as it should but all products are shown. Only matching products should be visible
– MazeyMazey
Nov 22 at 15:00












2 Answers
2






active

oldest

votes

















up vote
0
down vote













If I get your issue right - you want to move your "business logic" that happens on each input change to a separate function so that it can be executed also upon page reload.



...
// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});

// your business logic
function updateProducts() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.products >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
$('.products >div').show();

}
}

$('input[type="checkbox"]').change(function() {
// execute for each change
updateProducts();
});

// execute on page load
updateProducts()





share|improve this answer





















  • Hi @Peter Thanks for the answer. I have tried using your script but this does not work as expected. As you also state, what I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown. http//www.maxie.dk/filter2.php
    – MazeyMazey
    Nov 24 at 12:21


















up vote
0
down vote













I have re-write this such that it will update the localstorage and also check and show product that is save in localstorage. It will also remove or show product on checked. Check it out.



<div class="col-xs-12 col-sm-12">
<div class="breadcrumb" id="checkbox-container">
<h4 class="visible-xs"></h4>
<span class="hidden-xs"></span>

<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="1" value="1" />
<label for="1">Adidas</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="2" value="2" />
<label for="2">Reebok</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="3" value="3" /> <label for="3">Nike</label>
</div>

´
</div>
</div>

<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1" >
PRODUCT 1
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1">
PRODUCT 2
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="2"
>
PRODUCT 3
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="3"
>
PRODUCT 4
</div>
</div>


var checkedValues =JSON.parse(localStorage.getItem('checkboxValues')) || ,
$checkboxes = $("#checkbox-container input[type=checkbox]");

function loadPrevious(){
if(checkedValues.length>0){
$("div.products > div").hide();
$.each(checkedValues,function(i,v){
$("#checkbox-container input[type=checkbox]#" + v ).prop('checked',true);
$('.products >div[data-category=' + v + ']').show();
})
}

}

loadPrevious();

$checkboxes.change(function(e) {
e.preventDefault();
if($(this).prop("checked") == true){
checkedValues.push($(this).attr("id"));
loadPrevious();
localStorage.setItem("checkboxValues", JSON.stringify(checkedValues));
}else{
var checkId = $(this).attr("id"),
arr = JSON.parse(localStorage.getItem('checkboxValues'));
$('.products >div[data-category=' + checkId + ']').hide();
var newArr = $(arr).not([checkId]).get();
localStorage.setItem("checkboxValues", JSON.stringify(newArr));
}
});


See the demo here






share|improve this answer























  • Thanks for your answer. I have tried updating my example (maxie.dk/filter.php) with your script, but this does not work. The filter does not respond on first tick in a checkbox and also when refreshing the page the products are reset to show all products. Could you have a look?
    – MazeyMazey
    Nov 24 at 11:44










  • Check the jsFiddle sample
    – Mark Adesina Omoniyi
    Nov 25 at 5:50










  • Yes - very close @Mark. I am trying to reverse the logic with the checkboxes in your script so initially none is ticked but ALL products are shown. Only when activating/ticking the first checkbox, the sorting starts. I can't get your script to do so.
    – MazeyMazey
    Nov 25 at 8:19










  • If I get you right, you want to hide all products until checkbox is checked. If that is what you want then why do you now save checked history to localstorage?
    – Mark Adesina Omoniyi
    Nov 27 at 7:14










  • I do not want to hide products. I want the opposite. I want all products to be shown when page is initially loaded. When user ticks a checkbox only products with data-attribute value from checkbox is visible. What I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown.
    – MazeyMazey
    Nov 27 at 8:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432004%2fcheckbox-filter-products-with-jquery-and-keep-state-with-reload%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













If I get your issue right - you want to move your "business logic" that happens on each input change to a separate function so that it can be executed also upon page reload.



...
// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});

// your business logic
function updateProducts() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.products >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
$('.products >div').show();

}
}

$('input[type="checkbox"]').change(function() {
// execute for each change
updateProducts();
});

// execute on page load
updateProducts()





share|improve this answer





















  • Hi @Peter Thanks for the answer. I have tried using your script but this does not work as expected. As you also state, what I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown. http//www.maxie.dk/filter2.php
    – MazeyMazey
    Nov 24 at 12:21















up vote
0
down vote













If I get your issue right - you want to move your "business logic" that happens on each input change to a separate function so that it can be executed also upon page reload.



...
// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});

// your business logic
function updateProducts() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.products >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
$('.products >div').show();

}
}

$('input[type="checkbox"]').change(function() {
// execute for each change
updateProducts();
});

// execute on page load
updateProducts()





share|improve this answer





















  • Hi @Peter Thanks for the answer. I have tried using your script but this does not work as expected. As you also state, what I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown. http//www.maxie.dk/filter2.php
    – MazeyMazey
    Nov 24 at 12:21













up vote
0
down vote










up vote
0
down vote









If I get your issue right - you want to move your "business logic" that happens on each input change to a separate function so that it can be executed also upon page reload.



...
// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});

// your business logic
function updateProducts() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.products >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
$('.products >div').show();

}
}

$('input[type="checkbox"]').change(function() {
// execute for each change
updateProducts();
});

// execute on page load
updateProducts()





share|improve this answer












If I get your issue right - you want to move your "business logic" that happens on each input change to a separate function so that it can be executed also upon page reload.



...
// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});

// your business logic
function updateProducts() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.products >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
$('.products >div').show();

}
}

$('input[type="checkbox"]').change(function() {
// execute for each change
updateProducts();
});

// execute on page load
updateProducts()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 13:44









Peter Pajchl

2,2091624




2,2091624












  • Hi @Peter Thanks for the answer. I have tried using your script but this does not work as expected. As you also state, what I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown. http//www.maxie.dk/filter2.php
    – MazeyMazey
    Nov 24 at 12:21


















  • Hi @Peter Thanks for the answer. I have tried using your script but this does not work as expected. As you also state, what I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown. http//www.maxie.dk/filter2.php
    – MazeyMazey
    Nov 24 at 12:21
















Hi @Peter Thanks for the answer. I have tried using your script but this does not work as expected. As you also state, what I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown. http//www.maxie.dk/filter2.php
– MazeyMazey
Nov 24 at 12:21




Hi @Peter Thanks for the answer. I have tried using your script but this does not work as expected. As you also state, what I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown. http//www.maxie.dk/filter2.php
– MazeyMazey
Nov 24 at 12:21












up vote
0
down vote













I have re-write this such that it will update the localstorage and also check and show product that is save in localstorage. It will also remove or show product on checked. Check it out.



<div class="col-xs-12 col-sm-12">
<div class="breadcrumb" id="checkbox-container">
<h4 class="visible-xs"></h4>
<span class="hidden-xs"></span>

<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="1" value="1" />
<label for="1">Adidas</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="2" value="2" />
<label for="2">Reebok</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="3" value="3" /> <label for="3">Nike</label>
</div>

´
</div>
</div>

<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1" >
PRODUCT 1
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1">
PRODUCT 2
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="2"
>
PRODUCT 3
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="3"
>
PRODUCT 4
</div>
</div>


var checkedValues =JSON.parse(localStorage.getItem('checkboxValues')) || ,
$checkboxes = $("#checkbox-container input[type=checkbox]");

function loadPrevious(){
if(checkedValues.length>0){
$("div.products > div").hide();
$.each(checkedValues,function(i,v){
$("#checkbox-container input[type=checkbox]#" + v ).prop('checked',true);
$('.products >div[data-category=' + v + ']').show();
})
}

}

loadPrevious();

$checkboxes.change(function(e) {
e.preventDefault();
if($(this).prop("checked") == true){
checkedValues.push($(this).attr("id"));
loadPrevious();
localStorage.setItem("checkboxValues", JSON.stringify(checkedValues));
}else{
var checkId = $(this).attr("id"),
arr = JSON.parse(localStorage.getItem('checkboxValues'));
$('.products >div[data-category=' + checkId + ']').hide();
var newArr = $(arr).not([checkId]).get();
localStorage.setItem("checkboxValues", JSON.stringify(newArr));
}
});


See the demo here






share|improve this answer























  • Thanks for your answer. I have tried updating my example (maxie.dk/filter.php) with your script, but this does not work. The filter does not respond on first tick in a checkbox and also when refreshing the page the products are reset to show all products. Could you have a look?
    – MazeyMazey
    Nov 24 at 11:44










  • Check the jsFiddle sample
    – Mark Adesina Omoniyi
    Nov 25 at 5:50










  • Yes - very close @Mark. I am trying to reverse the logic with the checkboxes in your script so initially none is ticked but ALL products are shown. Only when activating/ticking the first checkbox, the sorting starts. I can't get your script to do so.
    – MazeyMazey
    Nov 25 at 8:19










  • If I get you right, you want to hide all products until checkbox is checked. If that is what you want then why do you now save checked history to localstorage?
    – Mark Adesina Omoniyi
    Nov 27 at 7:14










  • I do not want to hide products. I want the opposite. I want all products to be shown when page is initially loaded. When user ticks a checkbox only products with data-attribute value from checkbox is visible. What I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown.
    – MazeyMazey
    Nov 27 at 8:36















up vote
0
down vote













I have re-write this such that it will update the localstorage and also check and show product that is save in localstorage. It will also remove or show product on checked. Check it out.



<div class="col-xs-12 col-sm-12">
<div class="breadcrumb" id="checkbox-container">
<h4 class="visible-xs"></h4>
<span class="hidden-xs"></span>

<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="1" value="1" />
<label for="1">Adidas</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="2" value="2" />
<label for="2">Reebok</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="3" value="3" /> <label for="3">Nike</label>
</div>

´
</div>
</div>

<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1" >
PRODUCT 1
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1">
PRODUCT 2
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="2"
>
PRODUCT 3
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="3"
>
PRODUCT 4
</div>
</div>


var checkedValues =JSON.parse(localStorage.getItem('checkboxValues')) || ,
$checkboxes = $("#checkbox-container input[type=checkbox]");

function loadPrevious(){
if(checkedValues.length>0){
$("div.products > div").hide();
$.each(checkedValues,function(i,v){
$("#checkbox-container input[type=checkbox]#" + v ).prop('checked',true);
$('.products >div[data-category=' + v + ']').show();
})
}

}

loadPrevious();

$checkboxes.change(function(e) {
e.preventDefault();
if($(this).prop("checked") == true){
checkedValues.push($(this).attr("id"));
loadPrevious();
localStorage.setItem("checkboxValues", JSON.stringify(checkedValues));
}else{
var checkId = $(this).attr("id"),
arr = JSON.parse(localStorage.getItem('checkboxValues'));
$('.products >div[data-category=' + checkId + ']').hide();
var newArr = $(arr).not([checkId]).get();
localStorage.setItem("checkboxValues", JSON.stringify(newArr));
}
});


See the demo here






share|improve this answer























  • Thanks for your answer. I have tried updating my example (maxie.dk/filter.php) with your script, but this does not work. The filter does not respond on first tick in a checkbox and also when refreshing the page the products are reset to show all products. Could you have a look?
    – MazeyMazey
    Nov 24 at 11:44










  • Check the jsFiddle sample
    – Mark Adesina Omoniyi
    Nov 25 at 5:50










  • Yes - very close @Mark. I am trying to reverse the logic with the checkboxes in your script so initially none is ticked but ALL products are shown. Only when activating/ticking the first checkbox, the sorting starts. I can't get your script to do so.
    – MazeyMazey
    Nov 25 at 8:19










  • If I get you right, you want to hide all products until checkbox is checked. If that is what you want then why do you now save checked history to localstorage?
    – Mark Adesina Omoniyi
    Nov 27 at 7:14










  • I do not want to hide products. I want the opposite. I want all products to be shown when page is initially loaded. When user ticks a checkbox only products with data-attribute value from checkbox is visible. What I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown.
    – MazeyMazey
    Nov 27 at 8:36













up vote
0
down vote










up vote
0
down vote









I have re-write this such that it will update the localstorage and also check and show product that is save in localstorage. It will also remove or show product on checked. Check it out.



<div class="col-xs-12 col-sm-12">
<div class="breadcrumb" id="checkbox-container">
<h4 class="visible-xs"></h4>
<span class="hidden-xs"></span>

<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="1" value="1" />
<label for="1">Adidas</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="2" value="2" />
<label for="2">Reebok</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="3" value="3" /> <label for="3">Nike</label>
</div>

´
</div>
</div>

<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1" >
PRODUCT 1
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1">
PRODUCT 2
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="2"
>
PRODUCT 3
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="3"
>
PRODUCT 4
</div>
</div>


var checkedValues =JSON.parse(localStorage.getItem('checkboxValues')) || ,
$checkboxes = $("#checkbox-container input[type=checkbox]");

function loadPrevious(){
if(checkedValues.length>0){
$("div.products > div").hide();
$.each(checkedValues,function(i,v){
$("#checkbox-container input[type=checkbox]#" + v ).prop('checked',true);
$('.products >div[data-category=' + v + ']').show();
})
}

}

loadPrevious();

$checkboxes.change(function(e) {
e.preventDefault();
if($(this).prop("checked") == true){
checkedValues.push($(this).attr("id"));
loadPrevious();
localStorage.setItem("checkboxValues", JSON.stringify(checkedValues));
}else{
var checkId = $(this).attr("id"),
arr = JSON.parse(localStorage.getItem('checkboxValues'));
$('.products >div[data-category=' + checkId + ']').hide();
var newArr = $(arr).not([checkId]).get();
localStorage.setItem("checkboxValues", JSON.stringify(newArr));
}
});


See the demo here






share|improve this answer














I have re-write this such that it will update the localstorage and also check and show product that is save in localstorage. It will also remove or show product on checked. Check it out.



<div class="col-xs-12 col-sm-12">
<div class="breadcrumb" id="checkbox-container">
<h4 class="visible-xs"></h4>
<span class="hidden-xs"></span>

<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="1" value="1" />
<label for="1">Adidas</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="2" value="2" />
<label for="2">Reebok</label>
</div>
<div class="checkbox checkbox-success checkbox-inline ">
<input type="checkbox" id="3" value="3" /> <label for="3">Nike</label>
</div>

´
</div>
</div>

<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1" >
PRODUCT 1
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="1">
PRODUCT 2
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="2"
>
PRODUCT 3
</div>
</div>
<div class="products">
<div
class="col-xs-6 col-ms-4 col-sm-4 col-md-4 col-lg-3 padding-btm "
data-category="3"
>
PRODUCT 4
</div>
</div>


var checkedValues =JSON.parse(localStorage.getItem('checkboxValues')) || ,
$checkboxes = $("#checkbox-container input[type=checkbox]");

function loadPrevious(){
if(checkedValues.length>0){
$("div.products > div").hide();
$.each(checkedValues,function(i,v){
$("#checkbox-container input[type=checkbox]#" + v ).prop('checked',true);
$('.products >div[data-category=' + v + ']').show();
})
}

}

loadPrevious();

$checkboxes.change(function(e) {
e.preventDefault();
if($(this).prop("checked") == true){
checkedValues.push($(this).attr("id"));
loadPrevious();
localStorage.setItem("checkboxValues", JSON.stringify(checkedValues));
}else{
var checkId = $(this).attr("id"),
arr = JSON.parse(localStorage.getItem('checkboxValues'));
$('.products >div[data-category=' + checkId + ']').hide();
var newArr = $(arr).not([checkId]).get();
localStorage.setItem("checkboxValues", JSON.stringify(newArr));
}
});


See the demo here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 at 13:14

























answered Nov 23 at 16:01









Mark Adesina Omoniyi

162115




162115












  • Thanks for your answer. I have tried updating my example (maxie.dk/filter.php) with your script, but this does not work. The filter does not respond on first tick in a checkbox and also when refreshing the page the products are reset to show all products. Could you have a look?
    – MazeyMazey
    Nov 24 at 11:44










  • Check the jsFiddle sample
    – Mark Adesina Omoniyi
    Nov 25 at 5:50










  • Yes - very close @Mark. I am trying to reverse the logic with the checkboxes in your script so initially none is ticked but ALL products are shown. Only when activating/ticking the first checkbox, the sorting starts. I can't get your script to do so.
    – MazeyMazey
    Nov 25 at 8:19










  • If I get you right, you want to hide all products until checkbox is checked. If that is what you want then why do you now save checked history to localstorage?
    – Mark Adesina Omoniyi
    Nov 27 at 7:14










  • I do not want to hide products. I want the opposite. I want all products to be shown when page is initially loaded. When user ticks a checkbox only products with data-attribute value from checkbox is visible. What I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown.
    – MazeyMazey
    Nov 27 at 8:36


















  • Thanks for your answer. I have tried updating my example (maxie.dk/filter.php) with your script, but this does not work. The filter does not respond on first tick in a checkbox and also when refreshing the page the products are reset to show all products. Could you have a look?
    – MazeyMazey
    Nov 24 at 11:44










  • Check the jsFiddle sample
    – Mark Adesina Omoniyi
    Nov 25 at 5:50










  • Yes - very close @Mark. I am trying to reverse the logic with the checkboxes in your script so initially none is ticked but ALL products are shown. Only when activating/ticking the first checkbox, the sorting starts. I can't get your script to do so.
    – MazeyMazey
    Nov 25 at 8:19










  • If I get you right, you want to hide all products until checkbox is checked. If that is what you want then why do you now save checked history to localstorage?
    – Mark Adesina Omoniyi
    Nov 27 at 7:14










  • I do not want to hide products. I want the opposite. I want all products to be shown when page is initially loaded. When user ticks a checkbox only products with data-attribute value from checkbox is visible. What I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown.
    – MazeyMazey
    Nov 27 at 8:36
















Thanks for your answer. I have tried updating my example (maxie.dk/filter.php) with your script, but this does not work. The filter does not respond on first tick in a checkbox and also when refreshing the page the products are reset to show all products. Could you have a look?
– MazeyMazey
Nov 24 at 11:44




Thanks for your answer. I have tried updating my example (maxie.dk/filter.php) with your script, but this does not work. The filter does not respond on first tick in a checkbox and also when refreshing the page the products are reset to show all products. Could you have a look?
– MazeyMazey
Nov 24 at 11:44












Check the jsFiddle sample
– Mark Adesina Omoniyi
Nov 25 at 5:50




Check the jsFiddle sample
– Mark Adesina Omoniyi
Nov 25 at 5:50












Yes - very close @Mark. I am trying to reverse the logic with the checkboxes in your script so initially none is ticked but ALL products are shown. Only when activating/ticking the first checkbox, the sorting starts. I can't get your script to do so.
– MazeyMazey
Nov 25 at 8:19




Yes - very close @Mark. I am trying to reverse the logic with the checkboxes in your script so initially none is ticked but ALL products are shown. Only when activating/ticking the first checkbox, the sorting starts. I can't get your script to do so.
– MazeyMazey
Nov 25 at 8:19












If I get you right, you want to hide all products until checkbox is checked. If that is what you want then why do you now save checked history to localstorage?
– Mark Adesina Omoniyi
Nov 27 at 7:14




If I get you right, you want to hide all products until checkbox is checked. If that is what you want then why do you now save checked history to localstorage?
– Mark Adesina Omoniyi
Nov 27 at 7:14












I do not want to hide products. I want the opposite. I want all products to be shown when page is initially loaded. When user ticks a checkbox only products with data-attribute value from checkbox is visible. What I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown.
– MazeyMazey
Nov 27 at 8:36




I do not want to hide products. I want the opposite. I want all products to be shown when page is initially loaded. When user ticks a checkbox only products with data-attribute value from checkbox is visible. What I want to accomplish is having a page initially with all products listed (and checkboxes unchecked). When a checkbox is ticked, only products with the ticked checkboxes data value in attribute will be shown. When user refreshes page, the ticked checkboxes are remembered as well as only the products with checked data value in attribute will be shown.
– MazeyMazey
Nov 27 at 8:36


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432004%2fcheckbox-filter-products-with-jquery-and-keep-state-with-reload%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu