Return to Snippet

Revision: 56080
at March 8, 2012 11:29 by soup


Initial Code
<script type="text/javascript">
    jQuery(document).ready(function($){
        // Get current url
        // Select an a element that has the matching href and apply a class of 'active'. Also prepend a - to the content of the link
        var url = window.location;
        $('a[href="'+url+'"]').addClass('active');
        $('a[href="'+url+'"]').prepend('// ');
    });
</script>

Initial URL
http://theninthnode.com/2011/05/simple-jquery-active-menu-highlighter/

Initial Description
This is a very simple way of highlighting the current page of a menu. It uses javascipt to take the current url and cross checks it against any URLs that match in the menu. If one matches then add an ‘active’ class to it.
Be careful with #’s though. because a traling # in the URL would screw this up. Though you could always build in a function to strip out #’s before getting to the selector…
Please note: if at first this doesn’t work try using document ready or replacing $’s for jQuery..

Initial Title
Simple jQuery active menu highlighter

Initial Tags
jquery

Initial Language
jQuery