.filters
.filter.filterSport Спорт
.filter.filterFood Еда
.filter.filterWears Одежда
.wrapper
.link.isSport
.link.isSport
.link.isSport
.link.isFood
.link.isFood
.link.isFood
.link.isWears
.link.isWears
.link.isWears
var filterSport = document.querySelector('.filterSport'),
filterFood = document.querySelector('.filterFood'),
filterWears = document.querySelector('.filterWears'),
isSport = document.querySelectorAll('.isSport'),
isFood = document.querySelectorAll('.isFood'),
isWears = document.querySelectorAll('.isWears');
filterSport.onclick = function() {
[].forEach.call(isSport, function(e) {
if(e.target !== isSport) {
e.classList.add('hide')
}
});
// [].forEach.call(isWears, function(e) {
// e.classList.add('hide')
// });
}
<div class="filters">
<button class="filter" data-filter="isSport">Sport</button>
<button class="filter" data-filter="isFood">Food</button>
<button class="filter" data-filter="isWears">Wears</button>
</div>
<div class="wrapper">
<div class="link link_visible isSport">SPORT</div>
<div class="link link_visible isSport">SPORT</div>
<div class="link link_visible isSport">SPORT</div>
<div class="link link_visible isFood">FOOD</div>
<div class="link link_visible isFood">FOOD</div>
<div class="link link_visible isFood">FOOD</div>
<div class="link link_visible isWears">WEARS</div>
<div class="link link_visible isWears">WEARS</div>
<div class="link link_visible isWears">WEARS</div>
</div>
let filters = document.querySelector('.filters');
let filterButtons = filters.querySelectorAll('.filter');
let wrapper = document.querySelector('.wrapper');
let links = wrapper.querySelectorAll('.link');
filters.addEventListener('click', e => {
if (e.target.dataset.filter) {
if (e.target.classList.contains('filter_active')) {
e.target.classList.remove('filter_active');
links.forEach(item => item.classList.add('link_visible'));
} else {
filterButtons.forEach(btn => btn.classList.remove('filter_active'));
e.target.classList.add('filter_active');
links.forEach(item => {
item.classList.toggle(
'link_visible',
item.classList.contains(e.target.dataset.filter)
);
});
}
}
});