In my script below you can see what i have made. Idea is to make this filter products on page by adding values to array and check class later.
// FILTERS
var wybranyKolor = [];
var wybranyRozmiar = [];
function selectFilter(){
document.querySelectorAll(".grupa").forEach(group => {
var colorFilter = group.querySelector('.button-group-color').querySelectorAll('.button')
var sizeFilter = group.querySelector('.button-group-size').querySelectorAll('.button')
// KOLORY
colorFilter.forEach((button) => {
button.addEventListener("click", (e) =>{
colorFilter.forEach((thisColor) => {
thisColor.classList.remove("sg_active");
wybranyKolor.pop(filtr);
})
button.classList.add("sg_active");
var filtr = button.getAttribute('data-filter');
wybranyKolor.push(filtr);
showProducts();
console.log(wybranyKolor);
});
});
// ROZMIAR
sizeFilter.forEach((button) => {
button.addEventListener("click", (e) =>{
sizeFilter.forEach((thisSize) => {
thisSize.classList.remove("sg_active");
wybranyRozmiar.pop(filtr);
})
button.classList.add("sg_active");
var filtr = button.getAttribute('data-filter');
wybranyRozmiar.push(filtr);
showProducts();
console.log(wybranyRozmiar);
});
});
});
}
Here i get stuck, any ideas how to console.log values from array same as class in divs ?
Uncaught TypeError: boxPro.forEach is not a function
function showProducts(){
document.querySelectorAll('.grid').forEach(gridDiv => {
var boxPro = gridDiv.querySelector('.box_pro');
boxPro.forEach((elementDiv) => {
if(elementDiv.classList.contains(wybranyKolor)){
console.log('MAM ' + wybranyKolor);
}
});
});
}
You're trying to call an array prototype method on a primitive value. The querySelector() only selects the first element it finds, so you're not able to iterate over it. If you really need the forEach method, you may be able to do a querySelectorAll() and have it return an array with 1 element.