views:

543

answers:

1

Hi there, Im a noobo in the world of Javascript but am trying to get my heard around it (shouldn't take too)

Basicly what im trying to do is the following

I have a drop down box that when changed will change an image, which i have got to work using the following cose

<HTML><HEAD><TITLE>JS1</TITLE>
<script LANGUAGE="JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
<!--
image1 = new image(120,90)
image1.src = "desk1.gif"
image2 = new image(120,90)
image2.src = "desk2.gif"
image3 = new image(120,90)
image3.src = "desk3.gif"
image4 = new image(120,90)
image4.src = "desk4.gif"
function loadCatch(list)
{
var img = list.options[list.selectedIndex].value
document.thumbnail.src = eval(img + ".src")
}
</SCRIPT>
</HEAD>
<BODY>
<IMG SRC="desk1.gif" NAME="thumbnail" HEIGHT="90" WIDTH="120">
<FORM>
<SELECT NAME="catch" onChange="loadCatch(this)">
<OPTION VALUE="Image1">Bands
<OPTION VALUE="Image2">Clips
<OPTION VALUE="Image3">Lamp
<OPTION VALUE="Image4">Else
</SELECT>
</FORM>
</BODY>
</HTML>

The trouble is my form has 3-4 parts so when you click back to edit the form stays selected with the correct option selected but but the image shows the default image, i know this is because it is using an onChange event so i have investigated another way using the follwoing code:

<script language="JavaScript" type="text/javascript"> 
<!--
var dropdownIndex = document.getElementById('colorsselect').selectedIndex;
var dropdownValue = document.getElementById('colorsselect')[dropdownIndex].value;
document.write(dropdownValue);
//-->
</script>

Which displays the correct image if I click to go back but it isn't live (onChange) so the user gets doenst get immediate result showing the new image. I'm not sure how to combine the two pieces of code above, hope someone can help?

+2  A: 
Justin Ludwig
Wow works a treat, thanks for your help superb!!
If this answer solved your problem, you should click the check mark at "accept" the answer. This grants Justin (and you) rep points and lets other users know this question doesn't need to be answered again
Josh
Is there a way to have this code work for more than one drop down box on the same page, as i have at least 4 options that the user can choose? :)
Sure, for multiple `select` boxes, just give each `select` box a different `id` and `name`, but use the same `onchange` script. In the `body` `onload` script, call `loadCatch` for each `select` box; ie `"loadCatch(document.getElementById('catch-one'));loadCatch(document.getElementById('catch-two'));loadCatch(document.getElementById('catch-three'));"`.
Justin Ludwig