views:

75

answers:

2

I want to see if this is possible, i need to change a select element content to be displayed as divs using images just like a color picker. when the user selects clicks on a colored div i want an image to be displayed on the page. Also would i still be able to capture the selected item in a form hidden field?

It's a shirt building page, i have 12 colours, and 4 parts to a shirt. Any help, guidance would be appreciated.

My current code is below pretty basic.

function swapImage(){
 var image = document.getElementById("neck");
 var dropd = document.getElementById("imageneck");
 image.src = dropd.value;    

};

<select name="imageneck" id="imageneck" onChange="swapImage()"> 
   <option value="WHITE-4.png">White</option>
</select>

<div id="poloneck"><img id="neck" src="WHITE-4.png" /></div>

A demo of the page image based is here ..demo

Thanks

+1  A: 

http://api.jquery.com/val/

Check the demo for select box.

You can change your color or image based on select box selected value.

You can use jquery add function for adding css or

addClass function and removeClass for changing class

zod
One thing I discovered today - just fyi - is that you can also specify a timespan in the `addClass` or `removeClass` functions. For example `$(selector).addClass("classToAdd", 1000);`. This will make the element fade to that class. Alternatively replace add with remove to have it fade from that class to whatever properties it should have with the rest of the classes currently applied. Just thought I would mention it in case you could use it for this.
ClarkeyBoy
+1  A: 

I suggest you creating a <div> or <table> box for every picker. [Working demo]

Javascript

// instead of swapImage
function setImage(subject, color) {
 var image = document.getElementById(subject);
 image.src = color + ".jpg";  // e.g.: white.jpg
}

// general click event handler for changing color
function pickerClick(e) {
  // event -> which box -> which color
  e = e || event; 
  var target = e.target || e.srcElement;
  if ( target.nodeName.toUpperCase() != "TD" ) return;
  var color  = target.className;
  var picker = target.parentNode.parentNode;
  // change <input> field
  document.getElementById("imageneck").value = color;
  // change appearance
  setImage(picker, color);
}

// a specific click handler for neck
var collarbox = document.getElementById('collar');
collarbox.onclick = pickerClick;

HTML

<table class="picker" id="collar">
  <tr>
    <td class="white"></td>
    <td class="black"></td>
    <td class="red"></td>
    <td class="blue"></td>
  </tr>
</table>

CSS

.white { background: white }
.black { background: black; }
.red   { background: red; }
.blue  { background: blue; }
.picker { border: 1px solid #ccc; }
.picker td { width:30px; height: 30px; cursor: pointer; }

galambalazs
Thank you for your elegant solution...i just can't replicate the image to show. I guess i need my hand held or i might just miss the bus home
webb
Sorry to bother you, how do i add the images to the screen, just a bit confused. If you don't have time I'll understand
webb
in `setImage` you have `image.src = color + ".jpg";` That's where you assign an image to a part. You can change it so it matches your file names, it's just an example.
galambalazs
I tried and i failed miserably. I really want to use your solution but i just can't think properly to make it work. I'll need your help. I'm no good at js can you show me an example with the image changing?? Thanks
webb