Skip to content

Instantly share code, notes, and snippets.

Created February 12, 2021 21:38
What would you like to do?
var $variants = $('.variant-custom-fields');
var $form = $('form[action="/cart/add"]');
// Set the active variant on page load.
setActiveVariantCustomFields($form, $variants);
// Toggle display when dropdown changed in the "Add to Cart" feature.
$form.change(function() {
setActiveVariantCustomFields($form, $variants);
* Sets the active variant based on the "id" select dropdown in the add to cart form.
function setActiveVariantCustomFields($form, $variants){
var active_variant_id = $form.find('select[name="id"]').val();
$variants.filter('[data-variant-id="'+ active_variant_id +'"]').show();
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment