Results 1 to 2 of 2

Thread: #nav li Selected Menu Navigation Work but don't change page

  1. #1
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation #nav li Selected Menu Navigation Work but don't change page

    Hello,

    I'm trying to make a menu navigation but now that it's working it just don't change the page. When i click the page don't change but the clicked menu stays selected. What am i doing wrong?

    // CSS
    Code:
    #nav li a:hover {
    	background:#eaeaea; 
    }
    
    #nav li a.selected {
    	background:#eaeaea; 
    }
    //JAVASCRIPT
    Code:
    <script type="text/javascript">
    $(document).ready(function () {	
    	 $('#nav li').click(function(e){
    		e.preventDefault();
    		$('#nav li').removeClass("selected");
    		$(this).addClass('selected');
    	});
    });
    </script>
    // HTML + PHP
    Code:
    <ul id="nav">
            <li><a href="?mod=colecao"><h3><?=lg_colecao?></h3></a></li>
            <li><a href="#"><h3><?=lg_lookbook?></h3></a></li>
            <li><a href="?mod=empresa"><h3><?=lg_empresa?></h3></a></li>
            <li><a href="?mod=downloads"><h3><?=lg_downloads?></h3></a></li>
            <li><a href="?mod=noticias" class="noticias"><h3><?=lg_noticias?></h3></a></li>
            <li><a href="?mod=contactos" class="contactos"><h3><?=lg_contactos?></h3></a></li>
        </ul>
    
    <div class="contents">
    	 <?php include("mod/".$mod.".php"); ?>
    </div>
    Can anyone help me out? Thanks in advance
    Last edited by jscheuer1; 09-03-2012 at 01:08 AM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Using e.preventDefault() directs that the default action of clicking will not occur. In this case that default would be firing the link. It doesn't happen.

    Remove that line:

    Code:
    <script type="text/javascript">
    $(document).ready(function () {	
    	 $('#nav li').click(function(e){
    		e.preventDefault();
    		$('#nav li').removeClass("selected");
    		$(this).addClass('selected');
    	});
    });
    </script>
    And it will now work as expected. However, because you will now be moving/reloading (to) a new page, you need some other method for maintaining which li will have the selected class. PHP would probably be your best choice there. Check the $_GET['mod'] value and use it to assign the selected class to the appropriate li element.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 14
    Last Post: 08-17-2011, 05:47 PM
  2. CSS navigation to change background when being selected
    By mkf in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 08-16-2011, 03:22 PM
  3. Replies: 9
    Last Post: 04-17-2009, 08:36 AM
  4. DD site menu - local change to css of selected menu item
    By enile in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 05-23-2007, 11:33 AM
  5. Tab Content Script: trying to change colour for selected/not selected tab
    By justicefish in forum Dynamic Drive scripts help
    Replies: 5
    Last Post: 03-16-2006, 05:18 AM

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
  •