-2

I have a website with product catalog https://www.estelab.ru/cosmetics/zo-medical/

After click to the button "buy" in orange background you can see in console message ("addToCart works")

But this click works with first button.

var CartButton = document.querySelector(".eshop_js-add-to-cart");
CartButton.onclick = (function() {
  console.log("addToCart works");

});
  • document.querySelectorAll(".eshop_js-add-to-cart") dont help me in this question – Sadi Zeynalov Aug 27 '21 at 14:23
  • 1
    Welcome to SO! I recommend all new users visit [ask] for tips on forming questions in a manner that best enables the community to provide helpful assistance. For what it is worth, I am hesitant to click away to an unknown site for context on a question. Furthermore, if critical information for understanding the problem is on that site and the site gets fixed/updated, this post becomes useless for subsequent visitors. I'd recommend putting a [mcve] as a snippet _in the body of the question_, and include the reproduction steps and expected/actual behavior. Good luck and happy coding! – Alexander Nied Aug 27 '21 at 14:26
  • ok i will correct my question – Sadi Zeynalov Aug 27 '21 at 14:28

1 Answers1

1

You should use forEach

var CartButton = document.querySelectorAll(".eshop_js-add-to-cart");

CartButton.forEach((item) => {
  item.onclick = function () {
    console.log("addToCart works");
  };
});
<div class="eshop_js-add-to-cart">Click me!</div>
<div class="eshop_js-add-to-cart">Click me!</div>
<div class="eshop_js-add-to-cart">Click me!</div>
<div class="eshop_js-add-to-cart">Click me!</div>