Hi,
I’ve been trying to write some code for a custom liquid block for my website: mygarageposter.com (note: the custom liquid block is currently not visible on the live theme, as it isn’t working properly yet), so that products are cross-linked with buttons (same styling as the variant buttons) on the product page.
The code I’ve written so far seems to be working, as -when clicking the button- the page redirects to the other product page.
However, I would like to add some script(?) that allows to colorize the border of the button whenever it matches the current page (so it’s clear to the customer that that specific product is currently selected). I’ve been trying different approaches for the past couple of days, but none of them seem to work. In one of the approaches I tried to match the value of the button, with the page’s url-handle (see code below).
FYI: I have no coding experience. All code I’ve written thus far is by inspecting and combining the code of my variant buttons and the code of other shops that had a cross-linking feature.
Any help on the matter would be much appreciated! ![]()
<variant-radios id="variant-radios-template--16352980369582__main" class="no-js-hidden" data-section="template--16352980369582__main" data-url="/products/my-original-garage-poster" data-shopify-editor-block="{"id":"variant_picker","type":"variant_picker"}">
<style>
.active-button {
border: 2px solid #df2935;
}
</style>
<fieldset class="js product-form__input">
<legend class="form__label">Base</legend>
<input type="radio" id="template--16352980369582__main-1-0" name="Poster" value="my-original-garage-poster" form="product-form-template--16352980369582__main" class="product-form__input" onclick="window.location.href='https://mygarageposter.com/products/my-original-garage-poster';">
<label for="template--16352980369582__main-1-0">
Poster<span class="visually-hidden">Variant sold out or unavailable</span>
</label>
<input type="radio" id="template--16352980369582__main-1-1" name="Poster" value="my-original-garage-poster" form="product-form-template--16352980369582__main" class="product-form__input" onclick="window.location.href='https://mygarageposter.com/products/my-original-garage-poster';">
<label for="template--16352980369582__main-1-1">
Framed Poster<span class="visually-hidden">Variant sold out or unavailable</span>
</label>
<input type="radio" id="template--16352980369582__main-1-2" name="Poster" value="aluminum-print" form="product-form-template--16352980369582__main" class="product-form__input" onclick="window.location.href='https://mygarageposter.com/en/products/aluminum-print';">
<label for="template--16352980369582__main-1-2">
Alu Plate<span class="visually-hidden">Variant sold out or unavailable</span>
</label>
</fieldset>
<script type="application/json">
document.addEventListener('DOMContentLoaded', function () {
var radioButtons = document.querySelectorAll('.product-form__input input[type="radio"]');
var currentURL = window.location.href;
radioButtons.forEach(function (button) {
if (currentURL.includes(button.value)) {
button.parentElement.classList.add('active-button');
}
button.addEventListener('change', function () {
radioButtons.forEach(function (otherButton) {
otherButton.parentElement.classList.remove('active-button');
});
if (currentURL.includes(button.value)) {
button.parentElement.classList.add('active-button');
}
if (button.value === 'my-original-garage-poster') {
window.location.href = 'https://mygarageposter.com/products/my-original-garage-poster';
} else if (button.value === 'my-original-garage-poster') {
window.location.href = 'https://mygarageposter.com/products/my-original-garage-poster';
} else if (button.value === 'aluminum-print') {
window.location.href = 'https://mygarageposter.com/en/products/aluminum-print';
}
});
});
});
</script>
</variant-radios>