With the help of the SO commuinity I have managed to create a Javascript filtered list. What I would like to know is if it is possible to adapt what I have to enabled the direct linking to a filtered result. So all the filtering happens without leaving the page, which is great, however, I will need the ability to append the url so that I can navigate directly to a list of filtered results.
Here is my current JS:
const items = Array.from(document.querySelector(".artists-container").children)
const onClick = function() {
buttons.forEach((button) => { button.classList.remove("active") })
this.classList.add("active")
const target = this.getAttribute("data-target")
items.forEach((item) => { item.style.display = "none" })
items.filter(item => item.getAttribute("data-discipline").indexOf(target) >= 0 || target == "artists").forEach((item) => { item.style.display = "block" })
}
buttons.forEach((button) => { button.addEventListener("click", onClick) })
and here is my HTML:
<div class="filter-btn">
<ul id="buttons">
<li class="active" data-target="artists">All</li>
<li data-target="category1">Category 1</li>
<li data-target="category2">Category 2</li>
<li data-target="category3">Category 2</li>
</ul>
</div>
<div class="artists-container">
<article data-discipline="category1 category2">
<a href="#">
<div class="artist-details">
<p>Category 1, Category 2</p>
<h5>I appear in two categories</h5>
</div>
</a>
</article>
<article data-discipline="category3">
<a href="#">
<div class="artist-details">
<p>Category 3</p>
<h5>I appear in one category</h5>
</div>
</a>
</article>
Any help would massively be appreciated :)
Thanks