Shopify Development

Mastering Dynamic Fields: Show/Hide Textboxes with Checkboxes in Shopify OS 2.0

Hey there, fellow store owners and Shopify enthusiasts!

Want to enhance your Shopify product pages with dynamic fields? Imagine offering gift wrapping with a personalized note, an engraving option, or special instructions that only appear when a customer selects them. It's a fantastic way to declutter your page and offer a more tailored shopping experience.

However, implementing these dynamic elements in Shopify, especially with newer OS 2.0 themes, can be tricky. Our community member, dalilo, faced this exact challenge: their checkbox-controlled textbox worked in standalone HTML but failed in Shopify. This is a common hurdle, and you're not alone!

Shopify Liquid code with JavaScript for dynamic section loading
Shopify Liquid code with JavaScript for dynamic section loading

The OS 2.0 Challenge: Dynamic Sections and Disappearing JavaScript

dalilo's initial JavaScript was perfectly valid for a static page. The core issue, as Rob151 highlighted, lies with modern Shopify themes, particularly OS 2.0 themes like Dawn. These themes utilize dynamic sections. This means parts of your page – like the product form – can reload independently without a full page refresh when variants change or quantities update.

While great for speed, this dynamic reloading can silently remove any JavaScript event listeners you've attached to elements within those sections. Your script runs once, attaches the listener, and then if a section reloads, your listener is gone, leaving your functionality broken. It's a common characteristic of OS 2.0 themes, not a flaw in your code!

Solution 1: The Robust JavaScript Approach for OS 2.0 Themes

The solution involves ensuring your event listeners are re-initialized whenever a dynamic section loads. This is achieved by listening for the special shopify:section:load event, in addition to the standard DOMContentLoaded.

The Code:

Here’s the complete HTML and JavaScript snippet:


Where to Place This Code:

Place both the HTML and JavaScript within your product form, typically in your theme's main-product.liquid section file, inside the

tags. Alternatively, create a new snippet (e.g., snippets/custom-message-toggle.liquid) and include it there.

Important: The name="properties[Custom Message]" attribute on the text input is vital. This captures the custom message as a line item property, making it appear in the order details for your reference.

Solution 2: The Elegant CSS-Only Approach

For simpler scenarios where the checkbox and the textbox are direct siblings in the HTML, you can achieve the same show/hide effect with pure CSS. This method offers performance benefits and avoids JavaScript complexity.

The Code:

First, modify your HTML by removing the inline style="display: none;" from the customMessageField div:


Then, add this CSS to your theme's stylesheet (e.g., assets/base.css or assets/theme.css):

#customMessageField {
  display: none;
}

#showTextField:checked ~ #customMessageField {
  display: block;
}

How it Works:

The :checked pseudo-class targets the checkbox when selected. The ~ (general sibling combinator) then selects any #customMessageField element that is a sibling of the checked checkbox, making it visible. This is a clean, efficient solution for direct sibling relationships.

Key Considerations for Shopify Customizations

  • Placement: Always ensure your custom fields are within the main element of your product page for proper data submission.
  • Theme Compatibility: Test thoroughly in a theme duplicate. Minor adjustments might be needed based on your specific theme.
  • User Experience: Provide clear labels and instructions.
  • Testing: Always test across different browsers, devices, and after any theme or app updates.

At Shopping Cart Mover, we understand that customizing your Shopify store is key. Whether you're enhancing product pages with dynamic fields or considering a full migration to Shopify, these development insights are crucial. Custom code like this needs careful re-implementation when moving platforms or upgrading themes.

Conclusion

Adding dynamic elements like a checkbox-controlled textbox to your Shopify product page is achievable with either robust JavaScript or elegant CSS. By understanding Shopify's OS 2.0 behavior, you can implement powerful customizations that enhance your customer's journey and gather valuable information.

Empower your store with personalized shopping experiences. If you need expert assistance with complex Shopify customizations or a seamless e-commerce migration, Shopping Cart Mover is here to help!

Share:

Use cases

Explore use cases

Agencies, store owners, enterprise — find the migration path that fits.

Explore use cases