jquery - Alter filename of image, based on select box -
html:
<div id="product-main-image"> <img src="path/to/image.jpg"> </div> <select id="selection" name="selection"> <option value="someval1"> red </option> <option value="someval2"> blue </option> </select> js:
jquery("#selection").change(function() { var str = ""; jquery("#selection option:selected").each(function() { str += jquery( ).text() + " "; }); str = str.tolowercase().trim(); //red or blue var new_img = jquery("#product-main-image img").attr('data-zoom-image'); //here path/to/image.jpg //what now?? }); result: should path/to/image-red.jpg or path/to/image-red.jpg
note: extension can jpg, jpeg, png, gif etc.
you can simple regex based replace so:
var str = 'red'; var new_img = 'path/to/image.jpg'; new_img = new_img.replace(/(\.[a-z]{3,4})$/,'-'+str+'$1'); console.log(new_img); //"path/to/image-red.jpg" (you should able copy , paste of code test version console)
Comments
Post a Comment