How can I update the parameters of URL(window.history) based on multi groups of check-boxes if any of group been checked?
Like (if only the make selected)
JavaScript
x
2
1
www.mydomain/filters?make=toyota,honda
2
or if make and model is selected
JavaScript
1
2
1
www.mydomain/filters?make=toyota,honda&model=crv,cx5
2
JavaScript
1
33
33
1
$(document).ready(function() {
2
3
$("input:checkbox[name='make']").change(function() {
4
var makechecks = [];
5
$("input[type='checkbox']:checked").each(function() {
6
makechecks[makechecks.length] = this.value;
7
});
8
console.log( makechecks.join() );
9
});
10
11
12
$("input:checkbox[name='model']").change(function() {
13
var modelchecks = [];
14
$("input[type='checkbox']:checked").each(function() {
15
modelchecks[modelchecks.length] = this.value;
16
});
17
console.log( modelchecks.join() );
18
});
19
20
21
$("input:checkbox[name='year']").change(function() {
22
var yearchecks = [];
23
$("input[type='checkbox']:checked").each(function() {
24
yearchecks[yearchecks.length] = this.value;
25
});
26
console.log( yearchecks.join() );
27
});
28
29
function UpdateURL(){
30
window.history.pushState( {} , '', '');
31
}
32
33
});
JavaScript
1
18
18
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id="make">
3
<p>Toyota :<input type="checkbox" name="make" value="toyota"></p>
4
<p>Honda : <input type="checkbox" name="make" value="honda"></p>
5
<p>Mazda : <input type="checkbox" name="make" value="mazda"></p>
6
</div>
7
8
<div id="model">
9
<p>CRV :<input type="checkbox" name="model" value="crv"></p>
10
<p>RAV4 :<input type="checkbox" name="model" value="rav4"></p>
11
<p>CX5 :<input type="checkbox" name="model" value="cx5"></p>
12
</div>
13
14
<div id="year">
15
<p>2020 :<input type="checkbox" name="year" value="2020"></p>
16
<p>2019 :<input type="checkbox" name="year" value="2019"></p>
17
<p>2018 :<input type="checkbox" name="year" value="2018"></p>
18
</div>
Advertisement
Answer
Something like this?
JavaScript
1
15
15
1
const createURL = () => {
2
let urlSearch = new URLSearchParams()
3
let make = $("[name=make]:checked" ).map((_,chk) => chk.value).get()
4
let model = $("[name=model]:checked").map((_,chk) => chk.value).get()
5
let year = $("[name=year]:checked" ).map((_,chk) => chk.value).get()
6
if (make.length > 0) urlSearch.set("make", make.join(","))
7
if (model.length > 0) urlSearch.set("model", model.join(","))
8
if (year.length > 0) urlSearch.set("year", year.join(","))
9
const srch = urlSearch.toString();
10
console.log(srch);
11
// history.pushState({}, "Results for `Cars`", srch ? "?"+srch : "");
12
};
13
$(function() {
14
$("input:checkbox").on("change", createURL)
15
});
JavaScript
1
18
18
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id="make">
3
<p>Toyota :<input type="checkbox" name="make" value="toyota"></p>
4
<p>Honda : <input type="checkbox" name="make" value="honda"></p>
5
<p>Mazda : <input type="checkbox" name="make" value="mazda"></p>
6
</div>
7
8
<div id="model">
9
<p>CRV :<input type="checkbox" name="model" value="crv"></p>
10
<p>RAV4 :<input type="checkbox" name="model" value="rav4"></p>
11
<p>CX5 :<input type="checkbox" name="model" value="cx5"></p>
12
</div>
13
14
<div id="year">
15
<p>2020 :<input type="checkbox" name="year" value="2020"></p>
16
<p>2019 :<input type="checkbox" name="year" value="2019"></p>
17
<p>2018 :<input type="checkbox" name="year" value="2018"></p>
18
</div>