Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set type="button" on Add to Cart - Buttons (to prevent parent form submit on button click) #842

Open
JPustkuchen opened this issue Apr 11, 2023 · 0 comments

Comments

@JPustkuchen
Copy link

JPustkuchen commented Apr 11, 2023

Context:
Using iframe:false; and placing the buy button into an existing form.

Problem:
Generally works perfectly, but the button is missing the type="button" attribute, so if "Add to cart" is clicked, the form is being submitted

Proposed resolution:
Add type="button" attribute on the
<button class="shopify-buy__btn shopify-buy__beside-quantity" data-element="product.button">Add to cart</button> button.

Current result:
<button class="shopify-buy__btn shopify-buy__beside-quantity" data-element="product.button">Add to cart</button>

Expected result:
<button class="shopify-buy__btn shopify-buy__beside-quantity" data-element="product.button" type="button">Add to cart</button>

Temporary (dirty) workaround:

"events": {
        afterRender: function (component) {
  		  // Add type="button" to the buttons, so they won't submit the outer form!
          const shopifyBuyButtons = document.querySelectorAll(".shopify-buy__btn");
          shopifyBuyButtons.forEach((shopifyBuyButton) => {
            shopifyBuyButton.setAttribute('type', 'button');
          });
        },
      },
@JPustkuchen JPustkuchen changed the title Set type="button" on Add to Cart - Buttons Set type="button" on Add to Cart - Buttons (to prevent parent form submit on button click) Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant