473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How do I use a Combobox to change a displayed image on the screen

5
I found the following script that changes the text based on a user selection. I want to modify the code to change an image source based on the user selection.

I have been told I can do this with document.getElementById(image).src = "something.jpg"> but I am not sure how to incorporate this in the code.

Thanks for any help you can provide. I am new to this.


[HTML]<html><head><title>whatever</title>
<script type="text/javascript">

var textBlocks = new Array(
'Select a member',
'Member 1 text.',
'Member 2 text.');

function changetext(elemid) {
var ind = document.getElementById(elemid).selectedIndex;
document.getElementById("display").innerHTML=textB locks[ind];
}
</script>

</head><body>

<form>
<select id="whatever" onchange="changetext('whatever');">
<option value="0">Select</option>
<option value="1">Member1</option>
<option value="2">Member2</option>
</select><br>
</form>
<div id="display">Select a member</div>
</body></html>[/HTML]
Oct 11 '07 #1
7 3905
JosAH
11,448 Expert 8TB
Hi, this is a Javascript question, not a Java question. You posted your question in
the Java Forum where it doesn't belong. I move your question to the Javascript
forum. best of luck.

kind regards,

Jos
Oct 11 '07 #2
jzieba
5
Please help! The following script changes text based on a user selection in a Combobox. I want to modify the code to change a displayed image based on the user selection. For example if the user picks the first choice new1.jpeg is displayed. If the user picks the second choice then new2.jpeg is displayed.

I have been told I can do this with document.getElementById(image).src = "something.jpg"> but I am not sure how to incorporate this in the code.

Thanks for any help you can provide. I am new to this.



Code: ( html4strict )
[HTML]<html><head><title>whatever</title>
<script type="text/javascript">

var textBlocks = new Array(
'Select a member',
'Member 1 text.',
'Member 2 text.');

function changetext(elemid) {
var ind = document.getElementById(elemid).selectedIndex;
document.getElementById("display").innerHTML=textB locks[ind];
}
</script>

</head><body>

<form>
<select id="whatever" onchange="changetext('whatever');">
<option value="0">Select</option>
<option value="1">Member1</option>
<option value="2">Member2</option>
</select><br>
</form>
<div id="display">Select a member</div>
</body></html>[/HTML]
Oct 12 '07 #3
dmjpro
2,476 2GB
Please help! The following script changes text based on a user selection in a Combobox. I want to modify the code to change a displayed image based on the user selection. For example if the user picks the first choice new1.jpeg is displayed. If the user picks the second choice then new2.jpeg is displayed.

I have been told I can do this with document.getElementById(image).src = "something.jpg"> but I am not sure how to incorporate this in the code.

Thanks for any help you can provide. I am new to this.



Code: ( html4strict )
<html><head><title>whatever</title>
<script type="text/javascript">

var textBlocks = new Array(
'Select a member',
'Member 1 text.',
'Member 2 text.');

function changetext(elemid) {
var ind = document.getElementById(elemid).selectedIndex;
document.getElementById("display").innerHTML=textB locks[ind];
}
</script>

</head><body>

<form>
<select id="whatever" onchange="changetext('whatever');">
<option value="0">Select</option>
<option value="1">Member1</option>
<option value="2">Member2</option>
</select><br>
</form>
<div id="display">Select a member</div>
</body></html>

Use Code Tags buddy.
Simply update the array with "Image Sources" and then assign the "src" property with that "Image Source".

Expand|Select|Wrap|Line Numbers
  1. var imageBlocks = new Array( 
  2. 'new1.jpg', 
  3. 'new2.jpg', 
  4. 'new3.jpg');
  5.  
  6. var ind = document.getElementById(elemid).selectedIndex; 
  7. document.getElementById("display_image").src=imageBlocks[ind]; 
  8.  
  9.  
  10.  
Expand|Select|Wrap|Line Numbers
  1. <img id = "display_image">
  2.  
Debasis Jana
Oct 12 '07 #4
jzieba
5
Thanks DMJPRO for all the help.
Oct 12 '07 #5
jzieba
5
Thanks very much to DMJPRO! The code is working but when I first call up the page the image source is "Not Available". How do I initialize the image name?

Thanks again and here is the new code:

Expand|Select|Wrap|Line Numbers
  1. <html><head><title>whatever</title> 
  2. <script type="text/javascript"> 
  3.  
  4. var imageBlocks = new Array( 
  5. 'new0.gif', 
  6. 'new1.gif', 
  7. 'new2.gif');
  8.  
  9. function changetext(elemid) {  
  10. var ind = document.getElementById(elemid).selectedIndex; 
  11. document.getElementById("display_image").src=imageBlocks[ind];
  12. }
  13. </script> 
  14.  
  15. </head><body> 
  16.  
  17. <form> 
  18. <select id="whatever" onchange="changetext('whatever');"> 
  19. <option value="0">Select a member</option> 
  20. <option value="1">John Zieba</option> 
  21. <option value="2">Connie Zieba</option> 
  22. </select><br> 
  23. </form> 
  24. <img id="display_image"></div>
  25.  
  26. </body></html>
  27.  
Oct 12 '07 #6
acoder
16,027 Expert Mod 8TB
You'll need to set the src of the image.

The proper way to use code tags would be:

[CODE=javascript]
JavaScript code goes here...
[/code]
Oct 15 '07 #7
acoder
16,027 Expert Mod 8TB
Threads merged.
Oct 15 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: bds | last post by:
I'm new to .net 2.0 and I'm trying to figure out the best way to create a form that has a couple of controls and one of the controls is a combo box with a data bound list of items from another...
5
by: Steve | last post by:
I have an unbound combobox in the form header of a continuous form. The selection in the combobox sets the where clause in a querydef which determines QryPFrmInventoryManagement. The following code...
3
by: Pete | last post by:
I have a combobox which is used to select records, which is satisfactory at the moment. However, a second user is going to start using this database and there will be 1600 records. This makes...
1
by: Georges Heinesch | last post by:
Hi. I have a text field (txtTX) and a combobox (cboCB). Both controls are on 2 different subforms (SF1 and SF2) belonging to the main form (MF). MF -+- SF1 --- txtTX +- SF2 --- cboCB txtTX...
1
by: Andy Frank | last post by:
I have a tabular form that has a combo box on it that is reading from a database. When that combobox changes, I want it to lookup a database record to get a value and set the value into another...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
5
by: Eric A. Johnson | last post by:
Hi Everyone, I am at my wit's end here. I have a combobox (combyQueryTitle) that I need to use in order to select a query for my database project. Therefore, I am using the...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
4
by: JJGarcia | last post by:
Hi Everyone, I'll try to explain the process I'm following, I'm new to this so I'm triying the easy way first, probably the lasyest too! I created a new Project, drag in to it a SQLConnection,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.