Skip to content

Instantly share code, notes, and snippets.

@syahrasi
Forked from ttringas/Shopify Color Picker
Created September 4, 2013 03:26
Show Gist options
  • Save syahrasi/6432457 to your computer and use it in GitHub Desktop.
Save syahrasi/6432457 to your computer and use it in GitHub Desktop.
function makeColorButtons(){
var color_select = $('#product-select-option-0'),
color_list = $('#colors').html("");
color_select.find('option').each(function(i,el){
var element = $(el);
var color = element.val(),
color_attr = color.replace(/\s/g,"-"),
link = $(document.createElement('a'))
.attr('href',"#")
.attr('title', color),
swatch = $(document.createElement('li'))
.addClass('swatch color_hover')
.attr('id', color_attr)
.attr('title',color)
.attr('data-color', color)
.append(link)
.appendTo(color_list);
}
});
color_select.val("").hide().siblings().hide();
$('.swatch').on('click', function(e){
var swatch = $(this),
color = swatch.data('color'),
color_id = swatch.attr('id');
// make current swatch active
swatch.addClass('active').siblings().removeClass('active');
// update printed color
$('#current_color').html(color);
$('.image img').each(function(){
if ($(this).attr('alt') === color_id ){ $(this).parents('a').click(); return; }
});
// update the underlying hidden selector
color_select.val(color).change();
e.preventDefault();
});
$('.swatch').first().click();
$('.color_hover').tooltip({ placement: 'bottom'});
}
$(document).ready(function() {
// $('.swatch').first().click();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment