First, we need to go over one of my more mundane comments as a foundation:

Originally Posted by
Jesdisciple
In most cases, including this one, you shouldn't use an event-handler attribute; it makes a mess of the HTML. The same purpose may be accomplished, without mixing layout and behavior, by finding the DOM elements via document.getElementsByName when the page loads and assigning a function to the property of each which is spelled just like the attribute.
So now we have this HTML... (Note that I'm using the shorter naming convention because the longer one botches #10.)
Code:
<a href="javascript:;"><img id="thumb1" src="images/Aspen_Selections/_MG_5108_thumb.jpg" border="0"></a>
and this JavaScript...
Code:
var thumb1 = document.getElementById('thumb1');
thumb1.onclick = function (){
loadFull('image1');
selectThumb('thumb1');
};
Do you see the redundancy here? We twice go to all the trouble of finding the first element with id="thumb1". This is like looking up a citizen's unique identification number in a government database twice for two parts of the same task. If we keep it handy we can save time, so change selectThumb's argument to this and change selectThumb to the following. (this refers to whatever object owns the current function, i.e. the object of which the current function is a property. In this case, it's thumb1.)
Code:
function selectThumb(img){
for(a=1;a<75;a++){
if(document.getElementById('thumb'+a)){
document.getElementById('thumb'+a).style.opacity = '.5';
document.getElementById('thumb'+a).style.filter = 'alpha(opacity=50)';
}
}
img.style.opacity = '1';
img.style.filter = 'alpha(opacity=100)';
}
Now, to generalize that first script:
Code:
var thumbs = [];
var images = [];
for(var i = 1; i <= 10; i++){
thumbs[i] = document.getElementById('thumb' + i);
images[i] = document.getElementById('image' + i);
thumbs[i].onclick = function (){
loadFull(images[i]);
selectThumb(thumbs[i]);
};
};
But hold on... That doesn't work! By the time the thumb can be clicked, the loop has concluded and i is 10, so that code will always show the last image. Bugfix:
Code:
var thumbs = [];
for(var i = 1; i <= 10; i++){
thumbs[i] = document.getElementById('thumb' + i);
thumbs[i].image = document.getElementById('image' + i);
thumbs[i].onclick = function (){
loadFull(this.image);
selectThumb(this);
};
};
Can you work document.getElementsByName into that?
Bookmarks