Solved

change image source based on list/menu

Posted on 2009-04-01
7
168 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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.
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now