Forum Moderators: open

Message Too Old, No Replies

javascript function has to be clicked twice

         

JimHo

6:49 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



I am trying to do an image swap in Javascript with the following code:

<head>
<script type="text/javascript">
function update(series){
document.images['gimg'].src = "file:///C:/aaa-temp/best" + series + ".jpg";
}
</script>
</head><body>
<img id="gimg" src="file:///C:/aaa-temp/best1.jpg" width="510" height="300" >
<div id="desc">
<a href="#" onclick="update('1')" >1: picture 1 text</a>
<a href="#" onclick="update('2')" >2: picture 2 text</a>
</div>
</body>

When I first enter the page, I have to click one of the anchor references twice in order to make it work. Thereafter, the page works fine.

I put some alert boxes in the function, entered the page, and then clicked on the second reference. The image switched to best2.jpg, and then switched back to best1.jpg.

Any idea what I may have done wrong here?

Thanks Jim

Trace

7:33 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



you'll have better success using getElementByID as document.images returns a collection.

document.getElementById('gimg').src = "file:///C:/aaa-temp/best" + series + ".jpg";

Also, wouldn't hurt to add a return false to your links;

<a href="#" onclick="update('1'); return false;" >1: picture 1 text</a>
<a href="#" onclick="update('2'); return false;" >2: picture 2 text</a>

JimHo

7:51 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



Thanks Trace,

The return false seemed to clear it up..

Jim