Solved

change image source based on list/menu

Posted on 2009-04-01
7
171 Views
Last Modified: 2012-06-21
Hi Experts

Is it possible to change the source of an image based on what is chosen in a list menu? below is a snippet of code showing an image with an ID that needs it's source changed depending one if 1,2 or 3 is chosen in the list/menu.

Thanks for you time
<img src="images/getNameFromWhatIsSelectedInDropMenu.png" width="340" height="187" id="price-rise-overlay">
    <p>
        <label for="tfa_selectacolour">list1</label>
        <br>
        <select name="colourList" class="calc-colour" id="tfa_selectacolour">
            <option value="10" id="grey">item1</option>
            <option value="11" id="green">item3</option>
            <option value="12" id="cream">item2</option>
        </select>
    </p>

Open in new window

0
Comment
Question by:hnEE
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
Tomarse111 earned 500 total points
ID: 24037138
Try the code below:
<script type="text/javascript">
	function changeImg(val){
		if(val==10){document.getElementById('price-rise-overlay').src="/image/1.jpg"};
		if(val==11){document.getElementById('price-rise-overlay').src="/image/2.jpg"};
		if(val==12){document.getElementById('price-rise-overlay').src="/image/3.jpg"};			
	
	}
</script>
 
 
<img src="/assets/images/404.gif" width="340" height="187" id="price-rise-overlay">
    <p>
        <label for="tfa_selectacolour">list1</label>
        <br>
        <select name="colourList" class="calc-colour" id="tfa_selectacolour" onchange="changeImg(this.value)">
            <option  value="10" id="grey">item1</option>
            <option  value="11" id="green">item3</option>
            <option  value="12" id="cream">item2</option>
        </select>
    </p>

Open in new window

0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24037162
Hi hnEE,
Try this:

<form>
<img src="images/getNameFromWhatIsSelectedInDropMenu.png" width="340" height="187" id="price-rise-overlay">
    <p>
        <label for="tfa_selectacolour">list1</label>
        <br>
        <select name="colourList" class="calc-colour" id="tfa_selectacolour" onchange="javascript:changeopt(this);">
            <option value="0" id="1st">-- Select --</option>
            <option value="10" id="grey">item1</option>
            <option value="11" id="green">item3</option>
            <option value="12" id="cream">item2</option>
        </select>
    </p>
</form>
<script>
function changeopt(obj){
 
 var objImg=document.getElementById('price-rise-overlay');

 switch(parseInt(obj.value)){
   case 10:objImg.src='images/item1.png';break;
   case 11:objImg.src='images/item3.png';break;
   case 12:objImg.src='images/item2.png';break;
   default:objImg.src='images/getNameFromWhatIsSelectedInDropMenu.png';break;
 }
}
</script>
0
 

Author Closing Comment

by:hnEE
ID: 31565222
Awesome! Thank you so much.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24037174
Oopps, too late...
0
 

Author Comment

by:hnEE
ID: 24037638
Hi Tomarse111

I know you have answered this questions already... but is it possible to target the id of the select menu item?

I changed onchange="changeImg(this.value)" to onchange="changeImg(this.id)" but it didn't seem to work.

Thanks for your time.
0
 
LVL 15

Expert Comment

by:Tomarse111
ID: 24037657
Not quite sure what you mean sorry. Why would you want to target the ID of each option? What are you trying to do?
0
 

Author Comment

by:hnEE
ID: 24037675
this is actually on a dynamic site. the id of the select option will only come from a pool of 3 options - but the value (at the moment) comes from a pool of 16.

Just trying to write less code, and code for the future, and have values inserted into the script dynamically
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question