Are you an LLM? You can read better optimized documentation at /themes/tags/javascript.md for this page in Markdown format
javascript
Generates an HTML <script> tag containing the enclosed JavaScript.
liquid
{% javascript %}
JS_code
{% endjavascript %}1
2
3
2
3
JS_code: The JavaScript you want to run.
It's most useful inside a section or snippet, for the small amount of scripting that depends on the current settings. Anything reusable belongs in the assets directory, where it's cached across pages. See JavaScript and styles.
Example:
liquid
{% javascript %}
const root = document.getElementById('youcan-section--{{ section.id }}');
root?.querySelector('[data-toggle]')?.addEventListener('click', () => {
root.classList.toggle('is-open');
});
{% endjavascript %}1
2
3
4
5
6
7
2
3
4
5
6
7
Output
html
<script>
const root = document.getElementById('youcan-section--slideshow');
root?.querySelector('[data-toggle]')?.addEventListener('click', () => {
root.classList.toggle('is-open');
});
</script>1
2
3
4
5
6
7
2
3
4
5
6
7
Note: Scope your queries to the section's wrapper element rather than the whole document. Two copies of the same section can be on one page, and sections are added and removed while a seller is customizing.