I'm trying to search the SRC of a set of images then return all that matched a string. I'm building on a e-commerce system that is very restrictive, there is no way of linking an image to its variant so thats why I have to search a list of all images then return the matched src's. (I'm also using a rack of IF statements this is so the customer can use the WYSIWYG and add new colours)
jQuery(document).ready(function($) {
$('#product-variants-option-0').change(function() {
var select_value, keyword, new_src;
select_value = $(this).find(':selected').val();
if (select_value == "Kelly Green") { keyword = "kelly"; };
if (select_value == "Navy") { keyword = "navy"; };
if (select_value == "Gunmetal Heather") { keyword = "gunmetal"; };
if (select_value == "Olive") { keyword = "olive"; };
if (select_value == "Cocoa") { keyword = "cocoa"; };
if (select_value == "Black") { keyword = "black"; };
if (select_value == "Charcoal") { keyword = "charcoal"; };
if (select_value == "Dark Teal") { keyword = "teal"; };
if (select_value == "White") { keyword = "white"; };
if (select_value == "Black") { keyword = "black"; };
if (select_value == "Garnet") { keyword = "garnet"; };
if (select_value == "Charcoal Heather") { keyword = "charcoal-heather"; };
// Find the image using that `src`, note that we concatenate the value
// from `keyword`, rather than having it in a literal.
var new_src = $('#preload img[src*=' + keyword + ']').attr('src');
var large_src = new_src.replace('medium','large');
// Set the image's source.
$('div.image img').attr('src', large_src);
});
});
This works perfectly, for one image. But I need
var new_src = $('#preload img[src*=' + keyword + ']').attr('src');
as an array. then pass the first matched SRC to
$('div.image img').attr('src', large_src);
then all the rest to another div
$('div.thumbs img').attr('src', new_src);