views:

408

answers:

2

Hi all

I have this menu structure that is used to navigate through content slider panels.

 <div id="menu">
  <ul>
    <li><a href="#1" class="cross-link highlight">Bliss Fine Foods</a></li>
    <li><a href="#2" class="cross-link">Menus</a></li>
    <li><a href="#3" class="cross-link">Wines</a></li>  
    <li><a href="#4" class="cross-link">News</a></li>   
    <li><a href="#5" class="cross-link">Contact Us</a></li> 
  </ul>
</div>

I would like to loop through these elements and remove the highlight class and then add the highlight class to the current / last clicked menu item.

Any ideas? Any help would be greatly appreciated.

Thanks

Jonathan

+1  A: 
...your <head> and opening <body> tags...
<div id="menu">
   <ul>
     <li><a href="#1" class="cross-link highlight">Bliss Fine Foods</a></li>
     <li><a href="#2" class="cross-link">Menus</a></li>
     <li><a href="#3" class="cross-link">Wines</a></li>  
     <li><a href="#4" class="cross-link">News</a></li>   
     <li><a href="#5" class="cross-link">Contact Us</a></li> 
  </ul>
</div>
<script type="text/javascript">
(function( $ ) {
 $( "#menu ul li a" ).click( function() {
    $( this ).parent().parent().filter( 'a' ).removeClass( 'highlight' );
    $( this ).addClass( 'highlight' );
 });
})( jQuery );
</script>
</body>
</html>
Jacob Relkin
Thanks Jacob -how do I call this from the menu?
Jonathan Lyon
@Jonathan, just add this to your jQuery.ready code and you're all set.
Jacob Relkin
@jacob - sorry please excuse my ignorance but can you tell me how I add this to my header in terms of syntax
Jonathan Lyon
Or rather you can just stick this at the end of your `<body>` tag.
Jacob Relkin
Hi Jacob - perfect(ish) that works apart from it doesn't remove the class so every item is highlighted if I click on it
Jonathan Lyon
@Jonathan, Updated, try it now
Jacob Relkin
Hi jacob figured it out - your help in where to add the code along with edtsech's code worked like magic - thanks so much for your help!
Jonathan Lyon
+1  A: 

May be so?

$('#menu li a').click(function(){
  $('#menu li a').removeClass('highlight');
  $(this).addClass('highlight');
});
edtsech
Thanks that worked in combination with Jacob's help in where to add the code
Jonathan Lyon