I wrote this
hover the animation: animated slideIn
doesn’t trigger, while the console show notihing
let getAddToCart = document.querySelectorAll('.addToCart');
let getCartBadge = document.querySelector('.cartBadge');
getAddToCart.forEach((item) => {
item.addEventListener('click', function (e) {
e.preventDefault();
console.log('clicked');
// animate the cartBadge
getCartBadge.classList.add('animated', 'slideIn');
// other stuff
});
});
2
Answers
You can’t add a class name with space in it. Try adding them with two statements, or use
animated-slideIn
.This is a guess: You checked the scope inside your event listener? Try select getCartBadge using a variable self in external scope:
or using arrow function:
See more about scopes in:
https://www.programiz.com/javascript/variable-scope
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Shortened version if you want to explore: