0

Couldn't find a punctual answer for this simple task and your help is highly appreciated

We have an image we want to switch based on user's color selection.

Tried several methods, none worked.

This is the idea:

DarkAjax
  • 15,955
  • 11
  • 53
  • 65

2 Answers2

0
$('#YourButton').click(function() {
    var oldSrc = 'images/Image1.png';
    var newSrc = 'images/Image2.png';
    $('img[src="' + oldSrc + '"]').attr('src', newSrc);

});

Just change the image source with javascript by clicking your button with another color

Note: it´s jquery so you have to include the js file..

Marc Ster
  • 2,276
  • 6
  • 33
  • 63
  • Why deal with the old `src`? Just give the `` an `id`, use a selector, and call `attr('src', newSrc)` on it. You'll end up shooting yourself in the foot if you select it based on the old image's source. – Bucket Apr 16 '13 at 17:58
0

Just bind a click listener to your button and change the src attribute of your image.

$('#colorButton').click( function() { //choose a new color
    $('#imageIcon').attr('src', 'path/to/new/image.png'); //change the img's source
});

EDIT (response to questions): If you want this code to apply to all of your buttons, give each of your buttons a similar class instead of an ID:

<div class="colorButton"></div>

Then you can use the following selector to apply the above click listener to all of these divs:

$('.colorButton')

Naturally, you want to change your image as simply as possible. You could map all of your colors to their corresponding image file, but as far as design goes this might get messy and unwieldy. I would create a directory that stores all of your image files (for example, /your/color/swatches) and give each of them a name consistent with their color, like 'ff0000.png' for red, '0000ff.png' for blue, etc.

Why would we do this? So that we can switch your image based on the background-color attribute of your buttons. Let's say that you have the following buttons:

<div class="colorButton" style="background-color: '#ff0000'"></div>
<div class="colorButton" style="background-color: '#0000ff'"></div>

You can use the same click listener, but it will have to be modified a bit since we are mapping the background color to an image:

$('.colorButton').click( function() {
    var color = $(this).css('backgroundColor');
    //(You'll need to modify your color string here)
    $('#imageIcon').attr('src', 'your/color/swatches/' + color + '.png');
});

BUT this won't work yet. Since most browsers return "rgb(xx, yy, zz)" from .css('backgroundColor'), you need to convert that string into hex. This post on SO gives a more or less effective way to do so, but you'll need to modify it to fit your model where I have indicated.

Community
  • 1
  • 1
Bucket
  • 7,415
  • 9
  • 35
  • 45
  • Thank you DesertIvy! I think I'm close to implementing this one, however I am a total novice in JS. How can I add the selector to each color button and hide each respective image in the HTML? If you could provide a basic code example that would really help me. – AbeRock Apr 16 '13 at 21:18
  • Another thing. If we have 200 pages with different items. Does it means we have to enter all image paths in the JS file? Is there a smarter way to handle this, inserting the code in the HTML? – AbeRock Apr 16 '13 at 21:28
  • As well I was suggested CSS sprites could be use for this task. What would you recommend? – AbeRock Apr 16 '13 at 21:32
  • See my changes and let me know if I can clarify antything – Bucket Apr 17 '13 at 19:25
  • Ok now it makes more sense. I would like to know how you could replace an actual jpeg (say a photo of a bicycle) having multiple variations based on color (such as bike-orange.jpg and bike-green.jpg) Hopefully you can help me with a method to easily replace them based on the extension color "-green", this way such a script would work around the website, even where the prefix is 'car' instead of 'bike. Thank you again, really appreciate your clear explanations. – AbeRock Apr 17 '13 at 23:53
  • I think what you're asking is a little outside the scope of your question. For what you're looking for, I'd recommend taking a look at the `split`, `replace`, and `join` Javascript functions, and passing in your url. A more elegant solution would likely employ adding classes to your divs. – Bucket Apr 19 '13 at 17:33