When you click on a category, its name should turn red. But this only works for pre-rendered categories, and if you add a new one (it works with the input and ‘add btn’), the event handler does not work, nothing happens with the text
// * Adding active class for the cat
const addActiveClass = (elem) => {
elem.classList.add("active");
};
// ! Removing active class
const removeActiveClass = (catArr) => {
Array.from(catArr).forEach((elem) => {
elem.classList.remove("active");
});
};
Array.from(document.getElementsByClassName("categories__text")).forEach(
(elem) => {
elem.addEventListener("click", (e) => {
removeActiveClass(document.getElementsByClassName
("categories__text"));
addActiveClass(e.target);
});
}
);
console.log(Array.from(document.getElementsByClassName("categories__text")));
<ul class="categories__list">
<li class="categories__item">
<p class="categories__text active">All</p>
</li>
</ul>
2
Answers
Your issue stems from dynamically added elements not having the same event listeners as the ones initially present. The solution involves using event delegation, where you attach the event listener to a parent element and then verify if the clicked element matches your requirements. look here:
This way, even elements added later will have the click functionality because the event listener is on the parent, not the individual elements.
Hope this helps!
e.target
is the clicked element. Usee.target.closest()
to get the clicked.categories__text
if any.