?
Solved

change image source based on list/menu

Posted on 2009-04-01
7
Medium Priority
?
177 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

752 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