Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
I'm going to expand on my previous thread. After talking to you privately, I think I understand what you need. Let's take my previous example and expand on it:
HTML Code:
<head>
<script type="text/javascript">
function init() {
var myImg = document.getElementById('myimage');
document.getElementById('mylink').onclick = function() {
myImg.src = "wherever/img2.jpg";
}
}
window.onload=init; //wait for the DOM and all images to load, then execute function
</script>
</head>
<body>
<img src="whatever/img1.jpg" id="myimage">
<a href="#" id="mylink">Click to change image</a>
</body>
Let's break down each part of this.
First, the window.onload event activates the function init. Note that I use no parenthesis here. Although the function would still work, it would ruin potential usage of the 'this' keyword, which I don't want, in case I need it in the future.
The first thing that happens in the init() function is the creation of the variable(or object) myImg, which is bound together with getElementbyId and the id "myimage". This object now represents the image, which can be chained together with the 'src' attribute to change the image dynamically.
Next, I create a simple event on the anchor with the id of "mylink" by chaining it to the onclick method, then wrapping a statement in an anonymous function since it's just a one-line code. (myImg.src = "wherever/img2.jpg").
All this means is that if I click on the anchor "mylink", the image src of "myimage" will change from "whatever/img1.jpg" to "wherever/img2.jpg".
Hope this helps.
__________________
I build web things. I work for the startup Please login or register to view this content. Registration is FREE
.
Last edited by wayfarer07; 05-29-2008 at 11:06 PM..
Reason: error in code
|