jQuery

jQuery - Marking Current Link Active

jQuery - Marking Current Link Active

Have you ever wanted to have a link show as "Active" when a users is on that link? If you have a navigation at the top of your site and you want to visually indicate that the visitor is on that link, then you would usually add and "active" class to that link and style it appropriately.

This little snippet of code will do that.

$(document).ready(function(){
    var path = location.pathname.substring(1);
    if ( path ) {
        $('.header a[href$="' + path + '"]').addClass('active');
    }
});

location.pathname.substring(1); will return the url that you are on, subtract the hostname name.
a[href$="' + path + '"] looks for all anchor tags inside of the header block whose end match the path.

This is pure genius.

References:
http://docs.jquery.com/Tutorials:Auto-Selecting_Navigation

jQuery selector takes a second argument that sets context.

jQuery selector takes a second argument that sets context.

I was trying to concatenate a select to the almighty

this

in jQuery, but it just wasn't working.

I tried

$(this + 'input')

Didn't work, so I thought, maybe I need a space between this and input like so:
$(this + ' input')

No go..

Then I ran across this page.
http://stackoverflow.com/questions/306583/jquery-this-selector-and-children

Of course StackOverFlow has the answer.

The working result

$('input', this)

Perfect!!!

Developing in jQuery? Use $.dump instead of alert

Developing in jQuery? Use $.dump instead of alert

I just ran into the $.dump jQuery plugin. It sure makes troubleshooting and figuring out jQuery a whole lot easier. Here is how you include it in your page.

  • Download the plugin at here
  • Include it in your HTML like so
    <script type="text/javascript" src="jquery.dump.js"></script>
  • Now just include $.dump(object) somewhere and replace object with an object you want more information on

The pop-window you will get

Syndicate content