When specific variant is picked, textbox apears, but with text buttons dont work

When specific variant is picked, textbox apears, but with text buttons dont work

Eikva
Visitor
3 0 0

I have this code in main-product.liquid, when i choose specific variant text box appears, and then when customer inputs text, add to cart button or buy it now button dosent proceed to the next step, just refreshes the page. Thanks in advance

 

 

<script>
  document.addEventListener("DOMContentLoaded", function () {
    function setupTextBox() {
      const variantSelector = document.querySelector("[name='options[Ar reikalinga dovana viduje?]']");
      if (!variantSelector) return;

      let textBoxContainer = document.getElementById("customMessageContainer");

      if (!textBoxContainer) {
        textBoxContainer = document.createElement("div");
        textBoxContainer.id = "customMessageContainer";
        textBoxContainer.innerHTML = `
          <label id="customMessageLabel" for="customMessage" style="display:none; font-weight:bold; margin-top:10px; color:#f5e1d5;">
            Jūsų žinutė:
          </label>
          <textarea id="customMessage" 
            name="properties[Custom Message]"  
            style="display: none; 
            width:100%; 
            height:80px; 
            padding:10px;
            border:1px solid #f5e1d5;
            background:#3b1e1e; 
            color:#f5e1d5; 
            font-size:16px; 
            border-radius:5px;"></textarea>
        `;
        variantSelector.parentElement.after(textBoxContainer);
      }

      const textBox = document.getElementById("customMessage");
      const label = document.getElementById("customMessageLabel");

      function toggleTextBox() {
        if (variantSelector.value === "Žinutė viduje") {
          textBox.style.display = "block";
          label.style.display = "block";
          textBox.setAttribute("required", "true");
        } else {
          textBox.style.display = "none";
          label.style.display = "none";
          textBox.removeAttribute("required");
        }
      }

      variantSelector.removeEventListener("change", toggleTextBox); // Prevent duplicate event binding
      variantSelector.addEventListener("change", toggleTextBox);
      toggleTextBox();
    }

    // Prevent multiple mutation observers
    let observer;
    if (!window.__customTextBoxObserver) {
      observer = new MutationObserver(setupTextBox);
      observer.observe(document.body, { childList: true, subtree: true });
      window.__customTextBoxObserver = observer;
    }

    setupTextBox();
  });
</script>

 

 

 

Replies 2 (2)

TheScriptFlow_
Shopify Partner
848 63 106

Hey this is Qasim a Sr. Shopify Developer and official Shopify Partner.
Could you please share the link of your store so that I found what is the actual issue and then provide you solution code.

Thanks

- Need a Shopify Specialist? Chat on WhatsApp +923036471248 Or Email at info@thescriptflow.com

- Boost Your Sales with Affiliate Marketing - UpPromote: Affiliate & Referral


- If my solution was helpful, mark it as a solution and hit the like button!

Eikva
Visitor
3 0 0