SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery - Active Navigation with Class

    Hello everyone.

    I am attempting to set it so that upon clicking a link within this Joomla site (from the navigation menu), the link will become bold. I have already implemented CSS for ".active" and set the "LI" class to 'active' to the first class.

    I have done a search for the JQuery code to do this, but each example that I tried did not work at all. The class remained to be on the first link, home.

    Please see my existing code below and advise if you may please.

    URL of test page --
    http://bahawolf.com/davidlynn

    CSS for '.active' and 'nav' --
    Code:
    .nav {
    	background: url('../images/navbg.jpg') repeat-x;
    	border: 1px #a7a9ac;
    	padding-bottom: 3px;
    	padding-left: 10px;
    	margin-bottom: 30px;
    	height: 125px;	
    }
    
    .nav ul {
    	line-height:26px; 
    	margin: 0; 
    	padding: 0; 
    	list-style: none;
    }
    
    .active {
    font-weight:700;
    }
    The HTML for the menu --
    Code:
    <div class="nav" id="menu">
    <ul id="nav">
    <li class = "active"><a href="index.php">Home</a></li>
    <li><a href="index.php?option=com_content&amp;view=article&amp;id=47">Online Store</a></li>
    <li><a href="?url=keynote_topics">Keynote Topics</a></li>
    <li><a href="?url=contact">Contact Us</a></li>
    <li><a href="http://blog.david-lynn.com">Blog</a></li>
    </ul>
    </div>
    Existing example of JQuery Code --
    Code:
    $('.nav li a').click(function()
    {
      $('.nav li').removeClass('active');
      $(this).parent().addClass('active');
    });

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    When the link is clicked, the jQuery may well be adding the class, but after that the page is thrown away so that the clicked link can then load a new page.

    You need the loading page to know which one to make active.

    Here's an intro to the idea: Including Navigation Code On All Pages (Even With Current Navigation Highlighting!)

    and here's a video tutorial on putting it in to practice: Current Nav Highlighting: Using PHP to Set the Body ID
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •