header.ejs header.ejs

footer.ejs footer.ejs

delicacies.ejs delicacies.ejs

cart.js cart.js

I am trying to make the element("span.cartcount") display as it is set to display: none in the CSS file. It should be displayed when I click on addtocart button (a.addtocartbutton).

I will also be glad if I can get directions or an alternative way of going about shopping cart system with express.js

Thank you.

1

1 Answer

document.querySelector() returns just the first element matching the selector. You have a foreach() loop that creates multiple items, you need to add the event listener to all of them. So you need to use document.querySelectorAll() and loop over them.

let addToCartButtons = document.querySelectorAll("a.addtocartbutton"); let cartCount = document.querySelector("span.cartcount"); addToCartButtons.forEach(btn => btn.addEventListener("click", (event) => { event.preventDefault(); cartCount.style.display = "inline"; })); 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy