1

So, I'd like my breadcrumb nav to show the user which page he/she is on by having the be a different color according to the page they are on. I.e. if on home, the home link is gray whilst the others remain black.

To do this I've added the following code to my app:

$(function(){

    $('.breadcrumb li a').on('click', function(e){

        e.preventDefault(); // prevent link click if necessary?

        var $thisA = $(this);
        var $li = $thisA.parent('a');

        if (!$thisA.hasClass('active'))
        {
            $li.find('a.active').removeClass('active');
                $thisA.addClass('active');
        }

    })

})

However, with the above code it never releases the active class when I also click events for example they end up both just being gray. Also, the page doesn't switch it stays on home page but with home and events grayed out.

The css:

.breadcrumb li a.active {    color: #999999;}

The html

 <ul class="breadcrumb">
     <li>
       <a class="active" href="/profiles"> Home</a>
     </li>
     <li>
       <a class="active" href="/events"> Events</a>
     </li>
     <li>
       <a href="/inbox"> Messages</a>
     </li>
     <li>
       <a href="/welcome"> My Profile</a>
     </li>
   </ul>
Sonny Black
  • 1,585
  • 4
  • 23
  • 41

1 Answers1

1

Change

var $li = $thisA.parent('a');

to

var $li = $thisA.parents('ul');

because this line

 $li.find('a.active').removeClass('active');

looks for a child a.active and your original $li would be null because your original line is trying to find a a that is a parent of the clicked a.

EDIT

Resources on highlighting the current page:

http://hicksdesign.co.uk/journal/highlighting-current-page-with-css

$("a[href*='" + location.pathname + "']").addClass("current"); from highlighting the current page in a list of links using css / html

http://www.eznetu.com/current-link.html#

Community
  • 1
  • 1
TreeTree
  • 3,200
  • 3
  • 27
  • 39