Solved

select element, onChange needs to switch a photo

Posted on 2010-11-11
6
226 Views
Last Modified: 2012-05-10
See my code attached. I think I'm pretty close on this, but it isn't working. The onChange event on the <select> element is supposed to pass "img" plus the selected value of the <select> element to the switchphoto function. Which should change the source of the "mainimage" <img> on the page. However, switching the <select> menu has no effect. Suggestions? Thank you!
<script language="javascript" type="text/javascript">

if (document.images) {

var imgreb-053 = new Image()

imgreb-053.src = "http://www.lenoradame.com/lm_scans/reb-053.jpg"

var imgreb-053a = new Image()

imgreb-053a.src = "http://www.lenoradame.com/lm_scans/reb-053a.jpg"

var imgreb-053b = new Image()

imgreb-053b.src = "http://www.lenoradame.com/lm_scans/reb-053b.jpg"

var imgreb-053c = new Image()

imgreb-053c.src = "http://www.lenoradame.com/lm_scans/reb-053c.jpg"

}



function switchphoto(imgName) {

if (document.images)

mainimage.src = eval(imgName + '.src')

}

</script>



<img name="mainimage" src="http://www.lenoradame.com/lm_scans/reb-053.jpg" width="450" height="450" border="0" alt="" />



<select name="itemnum" onChange="switchphoto('img'+this.value);"><option value="reb-053">Silver - $112.00</option><option value="reb-053a">Blue/Green - $112.00</option><option value="reb-053b">Green/Purple - $112.00</option><option value="reb-053c">Red - $112.00</option></select>

Open in new window

0
Comment
Question by:bbdesign
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:CyberGhost
Comment Utility
things changed in the code:
1. ID added to the mainimage IMG
2. lookup in the switchphoto() function is using getElementById() to select the correct image on page
3. the way of accessing your global variables (imgreb-...) changed, so it does not need to use eval()


<script language="javascript" type="text/javascript">
if (document.images) {
var imgreb-053 = new Image()
imgreb-053.src = "http://www.lenoradame.com/lm_scans/reb-053.jpg"
var imgreb-053a = new Image()
imgreb-053a.src = "http://www.lenoradame.com/lm_scans/reb-053a.jpg"
var imgreb-053b = new Image()
imgreb-053b.src = "http://www.lenoradame.com/lm_scans/reb-053b.jpg"
var imgreb-053c = new Image()
imgreb-053c.src = "http://www.lenoradame.com/lm_scans/reb-053c.jpg"
}

function switchphoto(imgName) {
if (document.images)
document.getElementById('mainimage').src = window[imgName + '.src']);
}
</script>

<img name="mainimage" name="id" src="http://www.lenoradame.com/lm_scans/reb-053.jpg" width="450" height="450" border="0" alt="" />

<select name="itemnum" onChange="switchphoto('img'+this.value);"><option value="reb-053">Silver - $112.00</option><option value="reb-053a">Blue/Green - $112.00</option><option value="reb-053b">Green/Purple - $112.00</option><option value="reb-053c">Red - $112.00</option></select>
0
 

Author Comment

by:bbdesign
Comment Utility
Did you mean to put: name="id"
?
0
 

Author Comment

by:bbdesign
Comment Utility
Messed around with it, here is what I have, still not working:

<script language="javascript" type="text/javascript">
if (document.images) {
var imgreb-053 = new Image()
imgreb-053.src = "http://www.lenoradame.com/lm_scans/reb-053.jpg"
var imgreb-053a = new Image()
imgreb-053a.src = "http://www.lenoradame.com/lm_scans/reb-053a.jpg"
var imgreb-053b = new Image()
imgreb-053b.src = "http://www.lenoradame.com/lm_scans/reb-053b.jpg"
var imgreb-053c = new Image()
imgreb-053c.src = "http://www.lenoradame.com/lm_scans/reb-053c.jpg"
}

function switchphoto(imgName) {
if (document.images)
document.getElementById("mainimage").src = window['img'+imgName+'.src']
}
</script>

<img name="mainimage" id="mainimage" src="http://www.lenoradame.com/lm_scans/reb-053.jpg" width="450" height="450" border="0" alt="" />

<select name="itemnum" onChange="switchphoto('img'+this.value);"><option value="reb-053">Silver - $112.00</option><option value="reb-053a">Blue/Green - $112.00</option><option value="reb-053b">Green/Purple - $112.00</option><option value="reb-053c">Red - $112.00</option></select>
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Accepted Solution

by:
CyberGhost earned 500 total points
Comment Utility
sorry, I was in a bit of rush when answering and did not review the code closely
this is a working code, where:
1. you don't need to create new images, simply assign the image values to global text variables
2. you need to replace the minus sign (-) by an underscore (_) or JS will think you want to do mathematic operations with that pieces of variable (i.e. imgreb-053c is the same as doing: "imgreb - 053c = ???")

<script language="javascript" type="text/javascript">
if (document.images) {
var imgreb_053 = "http://www.lenoradame.com/lm_scans/reb-053.jpg";
var imgreb_053a = "http://www.lenoradame.com/lm_scans/reb-053a.jpg";
var imgreb_053b = "http://www.lenoradame.com/lm_scans/reb-053b.jpg";
var imgreb_053c = "http://www.lenoradame.com/lm_scans/reb-053c.jpg";
}

function switchphoto(imgName) {
if (document.images)
document.getElementById("mainimage").src = window[imgName]
}
</script>

<img name="mainimage" id="mainimage" src="http://www.lenoradame.com/lm_scans/reb-053.jpg" width="450" height="450" border="0" alt="" />

<select name="itemnum" onChange="switchphoto('img'+this.value);"><option value="reb_053">Silver - $112.00</option><option value="reb_053a">Blue/Green - $112.00</option><option value="reb_053b">Green/Purple - $112.00</option><option value="reb_053c">Red - $112.00</option></select>
0
 

Author Comment

by:bbdesign
Comment Utility
I think that does it, thanks! I wonder if the whole issue was the - vs. _ thing. Anyway, thanks again.
0
 
LVL 6

Expert Comment

by:CyberGhost
Comment Utility
well, it might've been... I've never used new Image() before, not even sure if that exists :-D
glad to help anyways :-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

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…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now