I'm currently working on creating a portfolio that includes button filters to toggle between black and white images and colored images based on the category selected.
While I have successfully implemented the functionality to change the images to black and white, I am facing an issue where the active state of the button does not persist after clicking it. Additionally, I am struggling to remove the black and white filter from the images within the selected category.
I suspect there might be an error in my code snippet as it is displaying an error message.
var container = document.getElementById('filters-button-group');
container.addEventListener('click', event => {
var activeItem = container.querySelector('.button-selected');
if (activeItem !== null) {
activeItem.classList.remove('button-selected');
}
if (event.target === activeItem) {
return;
}
event.target.classList.add('button-selected');
$('#img').css('filter', 'none');
});
button {
/* Button Styles */
border: 4px solid green;
border-top: 0;
border-right: 0;
font-size: 10px;
text-decoration: none;
color: green;
display: block;
margin-bottom: 22px;
}
.button:active,
.button-selected {
/* Selected Button Style */
background: rgba(8, 140, 126, 50%);
border: 4px solid green;
}
#portfolio { /* Positioning for Portfolio Section with Buttons */
text-align: center;
background: transparent;
position: absolute;
}
#portfolio img {
/* Black and White Image Filter */
filter: grayscale(100%) opacity(30%);
}
<section id="portfolio">
<div class="button-group filters-button-group">
<button class="button button-effect" data-filter=".A"><a href="#" class="opc-main-bg filter" >A</a></button>
<button class="button button-effect" data-filter=".B"><a href="#" class="opc-main-bg filter" >B</a></button>
<button class="button button-effect" data-filter=".C"><a href="#" class="opc-main-bg filter" >C</a></button>
<button class="button button-selected" data-filter="*"><a href="#" class="selected opc-main-bg filter">ALL</a></button>
</div>
<div
<div class="A"><img src="http://fakeimg.pl/365x365/ff0000/" width="50%" height="auto">
<div class="B"><img src="http://fakeimg.pl/365x365/ff0000/" width="50%" height="auto">
<div class="C"><img src="http://fakeimg.pl/365x365/ff0000/" width="50%" height="auto">
</section>