I keep encountering the issue links[i] is undefined
. Even after explicitly defining it, the error persists. Any thoughts on why this might be happening?
I am attempting to implement unobtrusive image rollovers for 5 links that I currently have.
function loadImages(){
path = 'uploads/Splash-4/nav/';
links = new Array();
for (i=1;i<=5;i++){
var id = "link-"+i;
var defaultState = '<img src="' +path+i+'.jpg" border="0" />';
links[i] = document.getElementById(id);
// Insert all image links into anchor
links[i].innerHTML = defaultState;
// Mouseover action
links[i].onmouseover = function() {
links[i].innerHTML = '<img src="' +path+i+'a.jpg" border="0" />';
}
// Mouseout action
links[i].onmouseout = function() {
links[i].innerHTML = defaultState;
}
}
}
window.onload = loadImages;
HTML:
<a href="?page=Profile" id="link-1"></a>
<a href="?page=for-sale" id="link-2"></a><br />
<a href="?page=testimonials" id="link-3"></a><br />
<a href="?page=free-home-appraisal" id="link-4" /></a><br />
<a href="?page=contact-me" id="link-5"></a><br />