Wednesday, August 1, 2012

HTML5+jQuery+CSS3 Highlight Selected Nav Item

After looking around the net I found that there were a number of ways to highlight the current item on a navigation menu. However, most of them seemed to be very rigid and written for a certain HTML structure. After finding this, I decided to write a simple jQuery script that allows the UX (User eXperience) to display as expected while using HTML5 coding practices. Here is the code for the script...

Script

/******************************************************
 * Listen for click event and set nav item to selected
 ******************************************************/
 var $navSets = $('nav'),
 //var $navSets = $('#navContainer'),
 $navLinks = $navSets.find('a');

 $navLinks.live('click', function(){
  var $this = $(this);
    
  if($this.hasClass('selected')) {
   return false;
  }

  var $navSet = $this.parents('nav');

  $navSet.find("a.selected").removeClass('selected');
  $this.addClass('selected');
 }); // End navLinks.live / click

The setup is pretty straight forward.

HTML Code

<nav>
    <ul>
        <li><a href='javascript:void(0);'>HOME</a></li>
        <li><a href='javascript:void(0);'>PORTFOLIO</a></li>
        <li><a href='javascript:void(0);'>ABOUT</a></li>
    </ul>
</nav>

The beauty of this is that you can add as many items that you want in the navigation and not have to worry about the page that's being called via AJAX to avoid a page refresh. Also, the nice thing about loading content with AJAX is that you only load the content you want to. While this isn't really SEO friendly, it does make for a great experience for the user.

8 comments:

Anonymous said...

I used to be recommended this web site by my cousin.
I'm not certain whether or not this publish is written by way of him as nobody else understand such specified about my trouble. You are amazing! Thanks!
Also visit my homepage ... fitness gyms

Anonymous said...

big beautiful women dating service arkansas http://loveepicentre.com/articles.php thai girls dating

Anonymous said...

Hello! I know this is kinda off topic nevertheless I'd figured I'd ask.
Would you be interested in trading links or maybe guest writing a blog post or vice-versa?

My site covers a lot of the same topics as yours and I believe we could greatly benefit from each other.
If you're interested feel free to send me an email. I look forward to hearing from you! Great blog by the way!
Also visit my blog post :: click the following website

Anonymous said...

It's perfect time to make some plans for the future and it's
time to be happy. I have learn this publish and if I could I desire to suggest you few attention-grabbing things or advice.
Perhaps you could write subsequent articles relating to this article.
I want to learn more issues approximately it!
Check out my weblog :: web hosting delhi

Anonymous said...

ebook ghost writing services http://audiobooksplanet.co.uk/it/Drawing-Scenery-Landscapes-and-Seascapes/p181850/ crack any ebook [url=http://audiobooksplanet.co.uk/Internet-Forensics/p202256/]how to ghostwrite an ebook[/url] hypnosis for change ebook

Anonymous said...

blackberry software for international calls http://buyoem.co.uk/it/product-15950/Uninstaller-1-10-Mac adobe photo shop element 2.00 software [url=http://buyoem.co.uk/es/category-200-214/Otros?page=3]symphony 8 software[/url] dvd encrypted burner software
[url=http://buyoem.co.uk/category-10/Internet?page=4]Internet - Software Store[/url] survealence camera software
[url=http://buyoem.co.uk/fr/product-37390/Easy-MP3-to-WAV-Maker-3-0][img]http://buyoem.co.uk/image/4.gif[/img][/url]

Anonymous said...

I Am 15 Years LateEvan Weber says," You're about five years late with this seo article.

Visit my blog post; search engine optimizatin

Anonymous said...

russian gay men internet dating scams http://loveepicentre.com/map/ webcam chat dating live online free
biker dating websites [url=http://loveepicentre.com/success_stories/]turkish dating site[/url] gavin degraw dating kendra jade
cebu dating filipina [url=http://loveepicentre.com/]dating vassar college[/url] fairbanks dating [url=http://loveepicentre.com/user/lovekristy/]lovekristy[/url] david eads dating service joplin mo