/    Sign up×
Community /Pin to ProfileBookmark

Newb at JS…need some help figuring out something "basic"…

So I’m basically trying to get 2 Javascript codes to work together. In this example, I can get the images to change when the selection is changed in the drop down menu (live example here: [url]http://modernash.com/t_image_change3.html[/url]):

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>Untitled Document</title>
<script type=”text/javascript”>
// preload images
var imgred = new Image().src = “image_change/jht.gif”
var imgblue = new Image().src = “image_change/jsht.gif”
var imggreen = new Image().src = “image_change/pht.gif”

function setImage(imageSelect) {
theImageIndex = imageSelect.options[imageSelect.selectedIndex].id;
if (document.images)
document.getElementById(“bob”).src = eval(“img” + theImageIndex);
}
</script>
</head>
<body>
<form name=”theForm” method=”post” action=”post.html”>
<table>
<tr>
<td>Images:</td>
<td>
<select name=”items” onChange=”setImage(this)”>
<option value=”0″>Please Select</option>
<option value=”1″ id=”red”>Java How-to</option>
<option value=”2″ id=”blue”>Javascript How-to</option>
<option value=”3″ id=”green”>Powerbuilder How-to</option>
</select>
</td>
<td><a id=”todd” href=”image_change/jht.gif”><img id=”bob” src=”image_change/jsht.gif” height=100 width=200/></a></td>
</table>
</form>
</body>
</html>[/CODE]

In this example, I can get get the linked image to change when a selection is made from the drop down menu. For example, if I choose “Javascript How-to” from the drop down menu, I can get it to link to the appropriate image (live example here: [url]http://modernash.com/t_href_change3.html[/url]):

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>Untitled Document</title>
<script type=”text/javascript”>
// preload images
var imgred = new Image().href = “image_change/jht.gif”
var imgblue = new Image().href = “image_change/jsht.gif”
var imggreen = new Image().href = “image_change/pht.gif”

function setImage(imageSelect) {
theImageIndex = imageSelect.options[imageSelect.selectedIndex].id;
if (document.images)
document.getElementById(“todd”).href = eval(“img” + theImageIndex);
}
</script>
</head>
<body>
<form name=”theForm” method=”post” action=”post.html”>
<table>
<tr>
<td>Images:</td>
<td>
<select name=”items” onChange=”setImage(this)”>
<option value=”0″>Please Select</option>
<option value=”1″ id=”red”>Java How-to</option>
<option value=”2″ id=”blue”>Javascript How-to</option>
<option value=”3″ id=”green”>Powerbuilder How-to</option>
</select>
</td>
<td><a id=”todd” href=”image_change/jht.gif”><img id=”bob” src=”image_change/jsht.gif” height=100 width=200/></a></td>
</table>
</form>
</body>
</html>
[/CODE]

The problem I’m having is getting both to work together. I tried to change the code in the head to allow for both to work at the same time (image change when the selection is chosen and the link to the appropriate image to change as well):

[CODE]// preload images
var imgred = new Image().src = “image_change/jht.gif”
var imgblue = new Image().src = “image_change/jsht.gif”
var imggreen = new Image().src = “image_change/pht.gif”

function setImage(imageSelect) {
theImageIndex = imageSelect.options[imageSelect.selectedIndex].id;
if (document.images)
document.getElementById(“bob”).src = eval(“img” + theImageIndex);
}
});
function setImage(imageSelect) {
theImageIndex = imageSelect.options[imageSelect.selectedIndex].id;
if (document.images)
document.getElementById(“todd”).href = eval(“img” + theImageIndex);
}
});
});[/CODE]

I hope someone has some pointers b/c I’m really horrible at JS and I’m a bit lost on this one.

Thanks!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 13.2009 — document.getElementById("todd").href = window["img" + theImageIndex];
Copy linkTweet thisAlerts:
@flinx777authorJan 13.2009 — Thank you for your response Fang. I implemented your response but it's still not working. This is the code that I implemented:

[CODE]// preload images
var imgred = new Image().src = "image_change/jht.gif"
var imgblue = new Image().src = "image_change/jsht.gif"
var imggreen = new Image().src = "image_change/pht.gif"

function setImage(imageSelect) {
theImageIndex = imageSelect.options[imageSelect.selectedIndex].id;
if (document.images)
document.getElementById("bob").src = eval("img" + theImageIndex);
}
});
function setImage(imageSelect) {
theImageIndex = imageSelect.options[imageSelect.selectedIndex].id;
if (document.images)
[COLOR="Red"]document.getElementById("todd").href = window["img" + theImageIndex];[/COLOR]
}
});
});[/CODE]


Did I misunderstand you?
Copy linkTweet thisAlerts:
@FangJan 13.2009 — function setImage(imageSelect) {
theImageIndex = imageSelect.options[imageSelect.selectedIndex].id;
if (document.images) {
document.getElementById("todd").href = window["img" + theImageIndex];
document.getElementById("bob").src = window["img" + theImageIndex];
}
}
Copy linkTweet thisAlerts:
@flinx777authorJan 13.2009 — Sweet...thanks for the quick reply...I really appreciate it so much. Yeh, that worked perfect...thanks a million...I owe you a beer! ?
×

Success!

Help @flinx777 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...