Jump to content

Javascript dropdown menu onChange image src


james909
Go to solution Solved by .josh,

Recommended Posts

I have the following Javascript that changes the images on screen as the dropdown menu chages:

Javascript

Code:
function hullchangeimage()
{
if (!document.images)
return
document.images.hullpicture.src=
document.hullgallery.changehull.options[document.hullgallery.changehull.selectedIndex].value
}

HTML Dropdown Menu:

Code:
<form name="hullgallery">
<select name="changehull" size="1" onChange="hullchangeimage()">
<OPTION value="Hull1">Hull1</option>
<OPTION value="Hull2">Hull2</option>
<OPTION value="Hull3">Hull3</option>
</select>
</form>

HTML image SRC:

Code:
<img class="hullbuild" src="build_pics/Hull1.png" name="hullpicture" width="500" height="250">

Problem is: this makes the picture the value of the select box, which for example is "Hull2", but the image scr is "build_pics/Hull2.png", so how do I add .png to the end of the value, and the folder infront, in the javascript. I can't change the values in the select options by adding .png as the value are needed for another function that requires just the value.

Link to comment
Share on other sites

FYI: You can make your life a little easier and pass a reference to the select field to the function

 

 

<select name="changehull" size="1" onChange="hullchangeimage(this);">

 

You can then simplify your function like so

 

function hullchangeimage(selectObj)
{
    if (!document.images) { return; }
    var imgName = selectObj.options[selectObj.selectedIndex].value;
    document.images.hullpicture.src = 'build_pics/' + imgName + '.png';
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.