Solved

change image source based on list/menu

Posted on 2009-04-01
7
169 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now