Skip to content Skip to sidebar Skip to footer

Highlight Current Link Javascript

Thanks to a lot of help earlier this week, I was able to really get somewhere on this 'dynamic' page im trying to create. But now I'm running into the following problem: I'm making

Solution 1:

In case you want to use only javascript for the described functionality, you can do it like this.

http://jsfiddle.net/T2Hv8/9/

I have trid to do the complete example, but as i am running short of time, i tell you what i have done till now.

on each click function return the object i.e "this"

Then on the function showDiv1 i have implemented a loop that parse all element with class rood and changes its background color.

functionshowDiv1(obj) {
var menus = document.getElementsByClassName("rood");
for (var i = menus.length - 1; i >= 0; i--)
    {
        //alert(menus[i].id);var elem = document.getElementById(menus[i].id);
        elem.style.backgroundColor = "transparent";
        elem.style.backgroundColor = "#E9EEBF";
    }
document.getElementById('store').innerHTML = 'Vestiging 1 is nu open';
obj.style.backgroundColor = "red";    
}

PS this is only in the showDiv1 function for now, but can be copied to other two too.

Now why this is not working correctly, i have passed the obj of anchor on click, while looping through class rood i get obj of span with class "rood"

What you need to do is, add class and id to the anchor instead of span and then run the same code.

I am sure it will work fine.

Regards

Solution 2:

I have finally figured out what OP wants, at least I think:

Check it out fiddle:http://jsfiddle.net/T2Hv8/10/

This is what you have to add in all ShowDiv function's (ShowDiv1, ShowDiv2, Showdiv3)

var spanID = obj.parentNode.id;
var newNode = document.getElementById(spanID);

var menus = document.getElementsByClassName("rood");
for (var i = menus.length - 1; i >= 0; i--)
    {   
        var elem = document.getElementById(menus[i].id);
        elem.style.backgroundColor = "transparent";
        elem.style.backgroundColor = "#ffffff";
    }

newNode.style.backgroundColor = "red";

Solution 3:

ids = ['id1', 'id2']
elems = [];

for(var i=0, len=ids.length; i<len; i++){
    elems.push(document.getElementById(ids[i])); 
    /* depend on what browsers you want to support you might prefere e.g. getElementsByClassName over above solution */
}

var select_one = function(e){
    for(var i=0, len=elems.length; i<len; i++){
        elems[i].style.backgroundColor = "transparent";
    }
    this.style.backgroundColor = "red";
    /* changing classes would be better */returnfalse;
}

for(var i=0, len=elems.length; i<len; i++){
    elems[i].onclick = select_one;
}

Solution 4:

I'd simply add and remove an additional class .rood_active instead of relying on the .rood:active to stick. Since you've got the same class name across your links, you need to loop through them by your base rood class name and add the new active class to it.

Additional CSS class:

.rood_active {
    color: #f30089;
} 

Javascript:

functionselectActiveLink() {
    //get all your rood class elements; 'links' will be an arrayvar links = document.getElementsByClassName('rood');
    for (var i = 0; i < links.length; i++) {
       var link = links[i];
       //reset all links to the base class 'rood'
       link.className = 'rood';
       link.addEventListener('click', function() {
           //this will add rood_active class (and change color) or only clicked spanthis.className = 'rood rood_active';
       });
    }
}

//then from within your other showDiv*() functions, call selectActiveLink()functionshowDiv2() {

    //all the other showDiv* function stuff hereselectActiveLink();
}

Post a Comment for "Highlight Current Link Javascript"