Javascript Noob Question. Need Help With Simple Script
Posted
by three3
on Stack Overflow
See other posts from Stack Overflow
or by three3
Published on 2010-04-18T14:52:13Z
Indexed on
2010/04/18
14:53 UTC
Read the original article
Hit count: 675
JavaScript
|javascript-events
Hi everyone,
I am new to JavaScript (only a couple of days of reading a book) and I am stuck on this code snippet. I have looked at it over and over again but cannot seem to figure out why it is not working. I am sure it is something really simple that I have just looked over but I really just need a fresh pair of eyes to look at this.
The code is supposed to update a placeholder image on a page without the page having to reload. But when I am clicking on the link of an image, it is taking me to the link where the image is located instead of replacing the placeholder image. Here is my HTML 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>Image Gallery</title>
<script type="text/javascript" src="scripts/showPic.js"></script>
</head>
<body>
<h1>Snapshots</h1>
<ul>
<li>
<a href="images/cat.jpg" onclick="showPic(this); return false;" title="A Cat">Cat</a>
</li>
<li>
<a href="images/night.jpg" onclick="showPic(this); return false;" title="Night">Night</a>
</li>
<li>
<a href="images/coke.jpg" onclick="showPic(this); return false;" title="Coke">Coke</a>
</li>
<li>
<a href="images/sport.jpg" onclick="showPic(this); return false;" title="Sports">Sport</a>
</li>
<li>
<a href="images/mnms.png" onclick="showPic(this); return false;" title="MnM's">MnM's</a>
</li>
<li>
<a href="images/kid.jpg" onclick="showPic(this); return false;" title="A Kid">Kid</a>
</li>
</ul>
<br />
<img id="placeholder" src="images/placeholder.jpg" alt="Place Holder Image" />
</body>
</html>
And here is the JavaScript function I am using to get this done:
<script type="text/javascript">
function showPic(whichpic) {
var source = whichpic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
placeholder.setAttribute("src",source);
}
</script>
Any help is greatly appreciated and thanks in advance for the help.
© Stack Overflow or respective owner