Tell me please, how can I do it for web-site on HTML CSS Javascript. If product is not in a shopping cart,
then show a text "Your cart is Empty." If the product is in the shopping cart, hide the text "Your cart is Empty.".
In Html there are several buttons with class .buttons on which the user clicks and adds products to the cart.
There are also added products in the shopping cart with class .basket__item. Buttons to delete items in cart with class .delete.
Lastly, the text with class .text_Empty_cart.
I was trying to do it through arrays and nothing happened. I tried through NodeList collections, but vainly.
function Cart_check() {
const btns_array = [...document.querySelectorAll(".buttons")];
const cart_Items_array = [...document.querySelectorAll(".basket__item")];
var text_Empty_cart = document.querySelector(".text_Empty_cart");
for (var i = 0; i < btns_array.length; i++) {
btns_array.addEventListener("click", () => {
for (var j = 0; j < cart_Items_array.length; i++) {
if (cart_Items_array[j] > 0) {
text_Empty_cart.style.display = "none";
} else {
text_Empty_cart.style.display = "block";
}
}
});
}
}
Cart_check();
Above one of my efforts. I’ll be glad for any help!
2
Answers
First of all, maybe you should use
j++
.Second, it looks the script will reset the visibility. Maybe you should:
You have not shared your HTML, so, for the moment we can only guess.
Below I out together a working snippet that will make the
.empty__basket
div disappear as soon as there are elements in the cart:The
Cart_check()
function now becomes almost trivial: It simply checks for an existence of a.basket__item
element and sets the visibility of the.text_Empty_cart
element accordingly.