Solved

select element, onChange needs to switch a photo

Posted on 2010-11-11
6
234 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
[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
  • 3
6 Comments
 
LVL 6

Expert Comment

by:CyberGhost
ID: 34115190
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
ID: 34115347
Did you mean to put: name="id"
?
0
 

Author Comment

by:bbdesign
ID: 34115391
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 6

Accepted Solution

by:
CyberGhost earned 500 total points
ID: 34116137
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
ID: 34117038
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
ID: 34119076
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

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.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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

636 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