A developer encountered a DOM selection error while building a JavaScript-triggered modal in Shopify. The modal snippet was rendered in theme.liquid with CSS to hide it by default, and a button was meant to toggle a CSS class to display it.
The Problem:
JavaScript couldn’t find the form element (.form-container-ja-outer-wrapper)
Console showed an error indicating the element wasn’t being selected
The test button was found successfully, but the modal form was not
Root Cause:
The JavaScript code executed before the DOM fully loaded. Since the button appeared earlier in the HTML than the modal snippet, only the button was accessible when the script ran.
Solutions Provided:
Move JavaScript code below both the button and modal HTML in source order
Wrap the code in a DOMContentLoaded event listener to wait for full DOM parsing
Use Shopify’s {% javascript %} tag or load the JS file with defer attribute
Add null checks (if (!testBtn || !form) return) before manipulating elements
Resolution:
The issue was resolved. The developer confirmed the explanations made sense and thanked the community for assistance.
Summarized with AI on November 4.
AI used: claude-sonnet-4-5-20250929.
I’ve created a section in Shopify that has a button element and some JavaScript that is intended to add the CSS class “.paragraph-class” to “.form-container-ja-outer-wrapper” in order to show the Snippet element.
The problem is that when browser runs you JS code the entire document is not processed yet, so form can not be found. testButton though is found because it is above your JS code. It is not Shopify-specific.
So i agree with Paul, your options are:
Move your JS to be below both button and popup HTML;
Use either document load or DOMContentLoaded event to run your JS code: