Change image with dropdown list?

Anyone know a link to some good plug and play code to accomplish this?

Basically I’d like to take a numerical value from a dropdown list and replace it as part of the imae path in the <img src> tag.

<script type=“text/javascript”>
function setImageSrc()
var x=document.images

<img src=“compman.gif” width=“107” height=“98” />

Select your favorite color:
<select name=“ColorID” onchange=“setSrc()”>
<option value = “151”>Kelly Green</option>
<option value = “161”>Maroon</option>
<option value = “171”>Orange</option>

Combined a couple scripts from to do this.
Here’s my problem now - As far as I can tell - this just changes the first image embedded into the page I need it more targeted ( and to make matters worse, the image I need to swap out is a table background.


Ok - I keep answering my own questions - I found this:

document.getElementById(shirtBack).style.background = “url(“images/ProductImage_”+cID+”.gif")";

Gave my table id=“shirtBack” and put that code in the setImageSrc() function.

Not working. What gives?

this may help



<script language="JavaScript" type="text/javascript">
// if you preload images they need to be loaded before they are required
// once in the cash they can be used as the original file name(&path) or as new Image.src ie SRC[1].src

ImgPreloadAry=new Array('One.gif','Two.gif','Three.gif','Four.gif');
SRCAry=new Array();

for (i=0;i<ImgPreloadAry.length;i++){
 SRCAry[i]=new Image();

function Cng(sel){


<img id=img src="C:/WebJavaScript/OurJavaScripts/StdImages/Cross.gif" width="100" height="100">
<table id=tbl width="200" height="200" border="1">

<select name="Sel" size="1" onchange="Cng(this);" >
<option value="One.gif">One</option>
<option value="Two.gif">Two</option>
<option value="Three.gif">Three</option>
<option value="Four.gif">Four</option>


The '(this) ’ can be useful in saving code it could be or this.value or whatever is applicable in this case it is the select object so you dont even need to give it a name.

I havent answered your question directly as you appear to like working things out your self(is good)

Thanks for your help!

I’m trying to do this in the simplest way possible, so I’m throwing out the ID for now and just trying to set a new backround image onChange, but even that’s not working.

A couple of questions:

Do I have to preload the images? There are a lot of them, so I’d rather not.

I’ve seen three references to the background:
and your style.backgroundImage

Which is/are correct?

I am testing this on Safari. What browser versions will getElementByID work with?