MorayM
MorayM

Reputation: 2687

jQuery attribute selector unrecognized expression error

I'm using the following code to select the list item for current page from a navigation unordered list, but I keep getting an unrecognized expression error on 'a[href$="{server-relative URL}"]'

I've checked matching quotes/brackets that other questions mention, and the link will not contain characters beyond the usual / & ? % The code I'm using is:

$(document).ready(function() {
    var pathname = window.location.pathname;
    var selector = "'a[href$=\"" + pathname + "\"]'";
    var listItem = $(selector).parent().parent();
    listItem.addClass('selected');
});

I'm using jQuery 1.8.2 (latest version). Thanks!

Upvotes: 1

Views: 5913

Answers (1)

Jacob
Jacob

Reputation: 78840

The problem is that you're surrounding your selector in single quotes. Just change this line:

var selector = "'a[href$=\"" + pathname + "\"]'";

...to this:

var selector = 'a[href$="' + pathname + '"]';

Upvotes: 6

Related Questions