I wrote a formatted price into the liquid variable disp_custom_price, then I wanted to put that formatted price into screen element “custom-price”.
Attempt 1) I tried inserting the liquid variable directly: document.getElementById(“custom-price”).innerHTML = {{disp_custom_price}};
Attempt 2) I tried the above commented out line putting the liquid variable into the JS variable: var mytext = {{disp_custom_price}};
but neither of those worked, so eventually I came up with the convoluted version above where I write the text into a hidden part of the screen, then read it back, then update it back to the element (further up the screen) where I really wanted it. The code here will be conditional, so that screen element will only be updated under certain circumstances, and I’ll want to update it again later if some options or selections are changed.
Can someone explain to me why my first two attempts failed?
This article was very helpful. We are using the Shopify 2.0 Canopy theme and there is a slight variation on the JavaScript syntax for the money format as follows:
Hi everyone, I’m trying to use liquid variable in js but didn’t get the results.
{% assign sorted_products_unsort = section.settings.collection.products %}