/    Sign up×
Community /Pin to ProfileBookmark

I need help with onClick image replacement!

Complete noob here. I’m simply making a page that displays image1. When the visitor clicks image1, the code needs to replace it with image2. I’ve searched hours on the web(including this forum) and haven’t been able to figure this out. Helps greatly appreciated!

Here’s my code:

[CODE]<html>
<head>
<script type=”text/javascript”>
function image()
{
document.getElementById(“changeimage”).innerHTML=”http://howimetyourotaku.files.wordpress.com/2011/05/ultimate-rage-face.jpg”;
}
</script>
</head>

<body id=”changeimage”>
<img src=”http://i3.kym-cdn.com/entries/icons/original/000/004/077/Raisins_Face.jpg” onClick=”image();”>
</body>
</html>[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@hyperionXSMar 04.2012 — [CODE]
<html>
<head>
<script type="text/javascript">
function image()
{
document.getElementById("changeimage")[COLOR="red"].src[/COLOR]="http://howimetyourotaku.files.wordpress.com/2011/05/ultimate-rage-face.jpg";
}
</script>
</head>

<body>
<img [COLOR="Red"]id="changeimage"[/COLOR] src="http://i3.kym-cdn.com/entries/icons/original/000/004/077/Raisins_Face.jpg" onClick="image();">
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@cootheadMar 04.2012 — Hi there a6turbo,

and a warm welcome to these forums. ?

If you are going to put the onclick event handler in the HTML code, then use this...
[color=navy]&lt;img src="http://i3.kym-cdn.com/entries/icons/original/000/004/077/Raisins_Face.jpg"
onclick="this.src='http://howimetyourotaku.files.wordpress.com/2011/05/ultimate-rage-face.jpg'"&gt;
[/color]

...but if you prefer the modern method of separating presentation from markup use this...
[color=navy]&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta name="language" content="english"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;

&lt;title&gt;&lt;/title&gt;

&lt;script type="text/javascript"&gt;

function init(){
document.getElementById('changeimage').onclick=function() {
this.src='http://howimetyourotaku.files.wordpress.com/2011/05/ultimate-rage-face.jpg';
}
}

window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);

&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div&gt;
&lt;img id="changeimage" src="http://i3.kym-cdn.com/entries/icons/original/000/004/077/Raisins_Face.jpg" alt=""&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
[/color]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@a6turboauthorMar 05.2012 — Worked perfect, thanks guys.
×

Success!

Help @a6turbo 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.18,
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,
)...