Adrian
Adrian

Reputation: 2291

Add class active when clicking menu link with JAVASCRIPT

HTML

<div id="top" class="shadow">
  <ul class="gprc"> 
   <li><a href="http://www.domain.com/">Home</a></li> 
   <li><a href="http://www.domain.com/link1/">Text1</a></li> 
   <li><a href="http://www.domain.com/link2/">Text2</a></li> 
   <li><a href="http://www.domain.com/link3/">Text3</a></li> 
   <li><a href="http://www.domain.com/link4">Text4</a></li> 
 </ul> 

Javascript

window.onload = setActive;
function setActive() {
    aObj = document.getElementById('top').getElementsByTagName('a');
    var found = false;
    for (i = 0; i < aObj.length; i++) {
        if (document.location.href.indexOf(aObj[i].href) >= 0) {
            aObj[i].className = 'active';
            found = true;
        }
    }
    if (!found) {
        aObj[0].className = 'active';
    }
}

The problem is that the menu home link remains selected or active all the time even if i click on other links and I would like to make it not selected on loading of the page and also to remain non-selected while other link that i clicked and i am on the specific landing page remains selected. Please only Javascript no JQUERY.

Upvotes: 1

Views: 994

Answers (2)

megawac
megawac

Reputation: 11353

function setActive() {
    var top = document.getElementById('top'),
        aObj = top.getElementsByTagName('a'),
        href = document.location.href,
        found = false;

    for (var i = 0; i < aObj.length || !found; i++) {
        if (href.indexOf(aObj[i].href) >= 0) {
            aObj[i].className = 'active';
            found = true;
        }
    }
    if (!found) {
        aObj[0].className = 'active';
    }

    //Listen for link clicks
    function listener(e) {
        if(e.target.tagName === "A") {
            for (var i = 0; i<aObj.length; i++) {//remove previous class
                aObj[i].className = "";
            }
            e.target.className = "active";
        }
    }
    if(top.addEventListener) {
        top.addEventListener(listener);
    } else if(top.attachEvent) {
        top.attachEvent(listener);
    }
}

You're going to need to listen to the click event so you can determine if one of your links is pressed. I'm going to do this using some simple delegation

Upvotes: 0

ccsakuweb
ccsakuweb

Reputation: 789

Try this:

window.onload = setActive;
function setActive() {
    var aObj = document.getElementById('top').getElementsByTagName('a');
    var found = false;
    for(var i=aObj.length-1; i>=1 && !found; i--) {
        if(document.location.href.indexOf(aObj[i].href)>=0) {
            aObj[i].className='active';
            found = true;
        }
    }
    //if you never want home selected remove the next
    if(!found && document.location.href.replace(/\/$/, "") == aObj[0].href.replace(/\/$/, ""))
         aObj[0].className = 'active';
}

With this way you start at the end of the list, and when you find a coincidence it stop the search of an active link.

I hope it helps you

Upvotes: 1

Related Questions