Theme app extension - Modify add to cart button

Shopify Partner
1 0 0

I'm building an app extension and trying to run a function (javascript) when someone click on the "Add to cart button". By using this on the theme code editor : 

var buttons = document.getElementsByTagName('button');
for(var i=0; i < buttons.length; i++) {
var btn = buttons[i];
if (btn.getAttribute("name") === "add") {
btn.addEventListener('click', (event) => {
alert("protection sold");
alert("button add is found");

Everything works fine. But when i'm trying to add this on a <script> ... </script> snippets liquid in my theme app extension, it seems that the button is not found... Does anyone have a solution?

Replies 0 (0)