Dave
Dave

Reputation: 108

If X matches href of Y add class 'active' - jQuery

I have links within an unordered list, when one is clicked it changes the url of a stylesheet link in the head. Using the cookie plugin the browser remembers which has been clicked.

I've managed to add an add/remove active class on click but I need to also have the correct active class on the corresponding a tag when the site is returned to. How do I use jQuery to check which stylesheet href has been used and assign the active class to the corresponding 'a' tag?

Current code is below.

Any help appreciated!

$(document).ready(function () {
    $("#text-size a").click(function() { 
        $("link.fontsize").attr("href",$(this).attr('rel'));
        $('ul li.active').removeClass('active');
        $(this).closest('li').addClass('active');
        $.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
        return false;
    });

Upvotes: 1

Views: 226

Answers (1)

MaK
MaK

Reputation: 606

$(document).ready(function () {
    $('#text-size a[rel="' + $('link.fontsize').attr('href') + '"]').closest('li').addClass('active');
});

Upvotes: 1

Related Questions