Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
Let me show you a super-simple example as to how to do this. Your code is about 5 times too long.
JavaScript
Code:
<script type="text/javascript">
window.onload = function() {
var a = document.getElementById("links").getElementsByTagName("a");
for(var i = 0; i < a.length; i++) {
a[i].onclick = function() {
document.getElementById("changing").src = this.href;
return false;
}
}
}
</script>
Then, in the html:
HTML Code:
<img id="changing" src="image1.jpg" />
<div id="links">
<ul>
<li><a href="image1.jpg">Image 1</a></li>
<li><a href="image2.jpg">Image 2</a></li>
<li><a href="image3.jpg">Image 3</a></li>
</ul>
</div>
Notice that I put the image paths right into the href of the link. This way it can be read by the function. The links are wrapped with an id of "links", and the image to be switched has an id of "changing". When the anonymous function bound to links through an onclick event is finished, it returns "false", so that the browser does not follow the link. This is all that is required for a simple image switcher.
|