When I click on the checkbox dynamic url create. If I refresh the page than checkbox show selected if brand name have some value my javascript code working fine.
Shop?brand=Emporio,Primigi%2088&category=&gender=&start=2000&end=4000
The problem is if brand name contain space like primigi 88, Lee Cooper than it select all the checkbox. I want to select the checkbox only that is in url.
$(function() {
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL =
decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
var brand = getUrlParameter('brand');
if (brand) {
var brand_array = brand.split(",");
for (var i = 0; i < brand_array.length; i++) {
$('input[type=checkbox][value=' + brand_array[i] +
']').attr('checked', true); // this will working fine if categories name not contain any space
}
}
var category = getUrlParameter('category');
if (category) {
var category_array = category.split(",");
for (var i = 0; i < category_array.length; i++) {
$('input[type=checkbox][value=' + category_array[i] +
']').attr('checked', true);
}
}
var gender = getUrlParameter('gender');
if (gender) {
var gender_array = gender.split(",");
for (var j = 0; j < gender_array.length; j++) {
$('input[type=checkbox][value=' + gender_array[j] +
']').attr('checked', true);
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="list-group">
<h3>Brands</h3>
<a href="#" class="list-group-item">
<input type="checkbox" class="item_filter brand" value="North East">North East</a>
<a href="#" class="list-group-item">
<input type="checkbox" class="item_filter brand" value="Emporio">Emporio</a>
<a href="#" class="list-group-item">
<input type="checkbox" class="item_filter brand" value="Lee Cooper">Lee Cooper</a>
<a href="#" class="list-group-item">
<input type="checkbox" class="item_filter brand" value="Primigi 88">Primigi 88</a>
<a href="#" class="list-group-item">
<input type="checkbox" class="item_filter brand" value="Us polo assn">Us polo assn</a>
</div>
$('input[type=checkbox][value=' + brand_array[i] + ']')to$('input[type=checkbox][value="' + brand_array[i] + '"]')