AlexC
AlexC

Reputation: 9661

jquery selectors

I want to add class to <a>, what has some url. For exampe :

$(document).ready(function() {

    var pathname = window.location.pathname;                    
    $(".navholder ul li a").each(function() {
        $(this).addClass('sel');
    });

});

I want to add "sel" class to <a> what has href=pathname.

Theoretically something like this :

$(".navholder ul li a").attr('href', pathname).addClass('sel');

Thanks!

Upvotes: 0

Views: 90

Answers (2)

mjangda
mjangda

Reputation: 370

Use this line:

$(".navholder ul li a[href='"+ pathname +"']").addClass('sel');

Upvotes: 2

gahooa
gahooa

Reputation: 137302

Take a look at the jQuery selector syntax:

$('.navholder ul li a [href=\\/foo\\/bar]').addClass('sel');

Note that you must escape / in jQuery expressions, and double-escape if it is a string literal.

Upvotes: 1

Related Questions