0
<button type="button" data-ab-testing="{{ settings.enabletest }}" class="Cart__Checkout Button Button--primary Button--full">
    <span>{{- 'cart.general.checkout' | t -}}</span>
    <span class="Button__SeparatorDot"></span>
    <span data-money-convertible>{{ cart.total_price | money_with_currency }}</span>
</button>

Here is a shopify button. Its like a regular button with some extra stuff thrown in to make it dynamic for templating purposes. For some reason the Javascript below, doesn't work on the button.

  var addToCartButton = document.querySelector("[data-ab-testing]");
  console.log(addToCartButton);
  var cartForm = document.querySelector(".Drawer__Content");
  addToCartButton.addEventListener("click", function(e) {
    if (addToCartButton.dataset.abTesting === "true") {
      console.log("true");
    }
  }, false);

Assuming dataset.abTesting === "true" has already been checked (and even removed completely to see if that was the issue) is there any reason that this wouldn't be firing? I have even confirmed the event listener is in fact attached to the event. Google Chrome has been problematic over the past couple days as a developer and I'm wondering if its Chrome acting up in some way. A version of this code worked on my peers computer a couple days ago and I'm at a total loss. I have tried adding an e and adding e.preventDefault() as well as removing the last false on the event listener. I'm about to name the function and add the directly to the button (onclick="funcName"), but even outside of the fact that this is not how I want to do this, I am also curious as to why this isn't working properly.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ellis hogan
  • 129
  • 9

1 Answers1

1

Since you mentioned that this button is dynamic, give this question a read and see if this helps:

That stackoverflow question refers to event delegation and bubbling, it might be something worth taking a peek at. Let me know if it doesn't work.

ChiefKeith
  • 83
  • 7
  • turns out it was just google chrome not clearing my cache... no matter how many times i told it too... It might be time to switch to duck duck go – ellis hogan Jun 19 '20 at 14:21
  • @ellishogan Chrome cache can be disabled via dev tools "Network" tab > Disable cache tickbox, you must keep the dev tools open for it to work though – revelt Aug 11 '21 at 12:16