I have created a blog post with three categories: (digital-marketing-category, tips-and-advice-category, cryptocurrency-category)
What I wanted to do is to filter it.
For example, if I want to see all the article for digital-marketing-category, I will click this button to show all the digital-marketing-category and hide other category.
Here’s a sample of my code. The problem with this is my filter is not working. What seems to be the problem?
I tried to add JavaScript to make it work but still it did not work. What seems to be the problem?
document.addEventListener("DOMContentLoaded", () => {
"use strict";
/**
* Article Category Filter
*/
const filterButtons = document.querySelectorAll('.blog-filters button');
const articleCards = document.querySelectorAll('.article-card');
filterButtons.forEach(button => {
button.addEventListener('click', () => {
const selectedCategory = button.getAttribute('data-category');
articleCards.forEach(card => {
const cardCategory = card.getAttribute('data-category');
if (selectedCategory === 'all' || selectedCategory === cardCategory) {
card.style.display = 'block';
} else {
card.style.display = 'none';
}
});
});
});
}); // this line added by community
<section id="blog" class="blog">
<div class="row">
<div class="col-md-8">
<div class="posts-list">
<div class="row">
<div class="col-lg-6 col-md-6 d-md-flex article-card" data-category="digital-marketing-category">
Blog Card
</div>
<!-- Article Card -->
<div class="col-lg-6 col-md-6 d-md-flex article-card" data-category="tips-and-advice-category">
Blog Card
</div>
<!-- Article Card -->
<div class="col-lg-6 col-md-6 d-md-flex article-card" data-category="cryptocurrency-category">
Blog Card
</div>
<!-- Article Card -->
</div>
</div>
</div>
<div class="col-md-4">
<div class="sidebar">
<h3 class="sidebar-title">Article Categories</h3>
<div class="blog-filters">
<button data-category="filter-all">All</button>
<button data-category="digital-marketing-category">Digital Marketing</button>
<button data-category="tips-and-advice-category">Tips & Advice</button>
<button data-category="cryptocurrency-category">Cryptocurrency</button>
</div>
</div>
</div>
</div>
</section>
2
Answers
In your code, you defined the
data-category
attribute for your Filter All button asdata-category="filter-all"
. So when the All button is clicked, theselectedCategory
variable in the JavaScript code will be set to "filter-all".However, in your if statement where articles are filtered by category, the statement compares against "all", which wouldn’t match "filter-all", so no articles will be displayed.
Here’s your corrected code:
Also, the JS you provided was missing a
});
at the very end, not sure if this is just you accidentally missing that line thought 🙂Already solve by @Beau.
You declare your button filter-all here
<button data-category="filter-all">All</button>
So in your condition
if (selectedCategory === 'all'
is wrong and it should beif (selectedCategory === 'filter-all'
as @Beau said.I just add snippet just in case.