View Full Version : Newbie problem with simple image swap

12-14-2010, 12:12 AM
Lets say I have two images in a box


<div id="box">

<img src="image1.jpg" id="image1" width="100" height="100" />
<img src="image2.jpg" id="image2" width="100" height="100" />



When I click on Image1 I want Image2 to get replaced with a new image, called Image3.

So I tried this code:

var image1 = document.getElementById("image1");
image1.onclick = image1_click ();

function image1_click () {
new_image= new Image
new_image = document.getElementById("image2");

But instead of replacing the image on click, from some reason it replaces image2 with image3 from start/load.

What should I do to make this work?
Thank you.

12-14-2010, 12:34 AM
Try this

function imageClick(ID,Image) {
new_image = document.getElementById(ID);
if(new_image) {
new_image.src = Image;

window.onload = (function(){
document.getElementById("image1").onclick = (function(){

12-14-2010, 12:51 AM
Thank you, that works perfectly :thumbsup: