javascripthtmlformsshopifyliquid

Pass value from Page form to javascript in Shopify liquid


I am using a link list in a Page template in Shopify and I need to pass the length of it to a javascript Snippet. I'm not sure how to do it.

The page template includes this line:

{% assign linklist = linklists['link-list-1'] %}

And the page includes a form with id="submit-table" similar to this code:

<form>
    <input type="submit" value="Add..." id="submit-table"/>
        ...
</form>

First, I need the form to contain the value of the length of the link list. Not sure how to do that.

Then I need to pass that length value to this script (which is a Snippet). Also, not sure how to do that.

<script type="text/javascript" charset="utf-8">
//<![CDATA[
// Including jQuery conditionally.
if (typeof jQuery === 'undefined') {
    document.write({{ "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" | script_tag | json }});
    document.write('<script type="text/javascript">jQuery.noConflict();<\/script>');
}
//]]>
</script>

<script>

$(document).ready(function () {
    $("#quantity-0").focus();    
    $("#submit-table").click(function(e) {     
        e.preventDefault();
        var toAdd = new Array();
        var qty;
        //I need the link list length value here
        for(i=0; i < length; i++){
        
            toAdd.push({
                variant_id: $("#variant-"+i).val(),        
                quantity_id: $("#quantity-"+i).val() || 0
            });
        }
        function moveAlong(){
            //I need the link list length value here
            if (toAdd.length) {
                var request = toAdd.shift();
                var tempId= request.variant_id;
                var tempQty = request.quantity_id;
                var params = {
                    type: 'POST',
                    url: '/cart/add.js',
                    data: 'quantity='+tempQty+'&id='+tempId,
                    dataType: 'json',
                    success: function(line_item) { 
                        //console.log("success!");
                        moveAlong();

                    },
                    error: function() {
                        //console.log("fail");
                        moveAlong();
                        
                    }
                };
                $.ajax(params);
            }
            else {              
                document.location.href = '/cart';
            }  
        };
        moveAlong();
    });
});
</script>

The script is included in my theme.liquid code as so:

{% include 'order-form-script' %}

References:

tetchi blog » Shopify Tutorial: How to Create an Order Form
http://www.tetchi.ca/shopify-tutorial-order-form/#comment-10129

https://gist.githubusercontent.com/Tetsuro/2932935/raw/568ccb0574f46eb450e298ab2ccb800e673d8fa2/jquery.order-form.js.liquid

https://gist.githubusercontent.com/Tetsuro/2932738/raw/7c0c5aea3cd59b35194f61e2c6c0b1c7f852f9fb/page.order-form.liquid


Solution

  • First, I need the form to contain the value of the length of the link list.

    Something like this?

    <form>
        <input type="submit" value="Add..." id="submit-table" />
        <input type="hidden" id="linklist-length" name="linklist-length" value="{{ linklists.link-list-1.links.size }}" />
        ...
    </form>
    

    Then I need to pass that length value to this script (which is a Snippet).

    Access the value via id (or if you want a more specific selector, see here):

    var length = $("#linklist-length").val();