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!
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!
Share Improve this question asked Nov 13, 2012 at 0:11 MorayMMorayM 2,6877 gold badges33 silver badges55 bronze badges 2-
1
Why
"'
, when only one delimiter (either"
or'
) should be enough? – raina77ow Commented Nov 13, 2012 at 0:15 - 1 Double check your quotes, that's the problem right there. – elclanrs Commented Nov 13, 2012 at 0:16
1 Answer
Reset to default 6The 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 + '"]';