Solved

change image source based on list/menu

Posted on 2009-04-01
7
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fix Form size HTML 16 31
Adding 1 the value of input "text" every 3 seconds 5 33
HTML editor custom button 3 59
How do i use the await event in php echo 5 19
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

726 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