Edit: I suspect the problem has to do that since all my # anchors are on the same page, unless I reload the page, JavaScript just doesn't know about my clicks on the link. I don't know how to solve this problem though...:(
I'm trying to make a clicked link stay blue if the section it points to is shown. This is the code (I put the Javascript code just before the closing body tag as suggested by this tutorial).
HTML
<ul id="menu">
<li><a href="#section1">section1</a></li>
<li><a href="#section2">section2</a></li>
<li><a href="#section3">section3</a></li>
JavaScript
links = document.getElementById("menu").getElementsByTagName("a");
for(i=0; i<links.length;i++) {
if(links[i].href == window.location.hash) {
links[i].className = "active";
}
}
CSS
a.active {
color:blue;
}
However it does not seem to work. Can anyone help? Thanks!
console.log(links[i].href+" "+window.location.hash)inside your loop but outside theifand see what you get.<a>tags are left unclosed with>