Whats up guys. I've created two links in an html document. The problem is the JavaScript below gives me a value of undefined after running the for loop. I know this has something to do with closures, which I don't have much experience with so I can't figure out why the value of links is undefined.
JavaScript:
window.onload = function getLinks(){
var links = document.links;
for(var i = 0, count = links.length; i < count; i++){
links[i].onclick = function(){
alert(links[i].href);
return false;
};
}
};
html:
<body>
<p><a href="linkOne.html" id="link" target="linkOne">Link B</a></p>
<p><a href="linkTwo.html" id="link" target="linkTwo">Link A</a></p>
<script src="js/handleLinks.js"></script>
</body>
</html>