4

I'm putting together a website, and want to display different products within different tabs. Each product is a buy button script c&p'd from Shopify, but the only product that loads is the one on the 'checked' tab.

Here's the live page: https://www.didsburyprint.com/mulit-product.html

<input id="tab1" type="radio" name="tabs" checked>
<label for="tab1">A3</label>

<input id="tab2" type="radio" name="tabs">
<label for="tab2">A2</label>

<input id="tab3" type="radio" name="tabs">
<label for="tab3">A1</label>

    
<section id="content1">
      
</section>

<section id="content2">
      
</section>

<section id="content3">
        
</section>

The page will only load one product, what can I do to load each script as the tab is opened?

Here's an example of the javascript I want to load in each tab.

<div id='product-component-0d047c59e1d'></div>
<script type="text/javascript">
/*<![CDATA[*/

(function () {
    var scriptURL = 'https://sdks.shopifycdn.com/buy-button/latest/buy-button-storefront.min.js';
    if (window.ShopifyBuy) {
        if (window.ShopifyBuy.UI) {
            ShopifyBuyInit();
        } else {
            loadScript();
        }
    } else {
        loadScript();
    }

    function loadScript() {
        var script = document.createElement('script');
        script.async = true;
        script.src = scriptURL;
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);
        script.onload = ShopifyBuyInit;
    }

    function ShopifyBuyInit() {
        var client = ShopifyBuy.buildClient({
            domain: 'didsbury-print.myshopify.com',
            storefrontAccessToken: '84d842627b8d1303f702af4934b58e9b',
        });

        ShopifyBuy.UI.onReady(client).then(function (ui) {
            ui.createComponent('product', {
                id: [2993409196113],
                node: document.getElementById('product-component-0d047c59e1d'),
                moneyFormat: '%C2%A3%7B%7Bamount%7D%7D',
                options: {
                    "product": {
                        "layout": "horizontal",
                        "variantId": "all",
                        "width": "100%",
                        "contents": {
                            "img": false,
                            "imgWithCarousel": true,
                            "variantTitle": false,
                            "description": true,
                            "buttonWithQuantity": false,
                            "quantity": false
                        },
                        "styles": {
                            "product": {
                                "text-align": "left",
                                "@media (min-width: 601px)": {
                                    "max-width": "100%",
                                    "margin-left": "0",
                                    "margin-bottom": "50px"
                                }
                            },
                            "button": {
                                "background-color": "#ff4328",
                                ":hover": {
                                    "background-color": "#e63c24"
                                },
                                ":focus": {
                                    "background-color": "#e63c24"
                                }
                            },
                            "title": {
                                "font-size": "26px"
                            },
                            "price": {
                                "font-size": "18px"
                            },
                            "compareAt": {
                                "font-size": "15px"
                            }
                        }
                    },
                    "cart": {
                        "contents": {
                            "button": true
                        },
                        "styles": {
                            "button": {
                                "background-color": "#ff4328",
                                ":hover": {
                                    "background-color": "#e63c24"
                                },
                                ":focus": {
                                    "background-color": "#e63c24"
                                }
                            },
                            "footer": {
                                "background-color": "#ffffff"
                            }
                        }
                    },
                    "modalProduct": {
                        "contents": {
                            "img": false,
                            "imgWithCarousel": true,
                            "variantTitle": false,
                            "buttonWithQuantity": true,
                            "button": false,
                            "quantity": false
                        },
                        "styles": {
                            "product": {
                                "@media (min-width: 601px)": {
                                    "max-width": "100%",
                                    "margin-left": "0px",
                                    "margin-bottom": "0px"
                                }
                            },
                            "button": {
                                "background-color": "#ff4328",
                                ":hover": {
                                    "background-color": "#e63c24"
                                },
                                ":focus": {
                                    "background-color": "#e63c24"
                                }
                            }
                        }
                    },
                    "toggle": {
                        "styles": {
                            "toggle": {
                                "background-color": "#ff4328",
                                ":hover": {
                                    "background-color": "#e63c24"
                                },
                                ":focus": {
                                    "background-color": "#e63c24"
                                }
                            }
                        }
                    },
                    "productSet": {
                        "styles": {
                            "products": {
                                "@media (min-width: 601px)": {
                                    "margin-left": "-20px"
                                }
                            }
                        }
                    }
                }
            });
        });
    }
})();
/*]]>*/
</script>
HoRn
  • 1,458
  • 5
  • 20
  • 25
  • you want to show and hide content based on what is selected? – Dev Man May 25 '19 at 19:53
  • yes, but it will only load content in the tab which is open by default – Matt Dawson May 25 '19 at 20:01
  • see https://stackoverflow.com/questions/18307323/how-to-show-hide-an-element-on-checkbox-checked-unchecked-states-using-jquery – Dev Man May 25 '19 at 20:06
  • Thanks, not sure if it answers the question though (sorry new to this). Showing and hiding content works, but the JavaScript within the tabs which are default hidden does not load when the tab is opened. – Matt Dawson May 25 '19 at 20:14
  • see for appending scripts: https://stackoverflow.com/questions/9413737/how-to-append-script-script-in-javascript – Dev Man May 25 '19 at 20:23

0 Answers0