Personalized checkout and custom promotions with Shopify Scripts
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>
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
June brought summer energy to our community. Members jumped in with solutions, clicked ...
By JasonH Jun 5, 2025Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025