TDG TDG - 1 year ago 284
jQuery Question

Syntax error, unrecognized expression for href

When i add below script and run. getting,

Uncaught Error: Syntax error, unrecognized expression: ul li

Not sure which double quote causing the issue. can any one pls help to solve the issue?


<li class="slist selected" id="id1"><a href="#!id10">Test1/a></li>
<li class="slist" id="id2"><a href="#!id20">Test2</a></li>
<li class="slist" id="id3"><a href="#!id30">Test3/a></li>


$(document).ready(function () {
var id = "#!" + window.location.href.split("!")[1];
if ($("ul li a[href=" + id + "]").length) {
} else {

Answer Source

You need to enclose special characters in quotes when using a attribute based selector.

if ($('ul li a[href="' + id + '"]').length) {

Your version of selector would result

if ($("ul li a[href=#!...]").length) {

The #! will throw unrecognized expression.

My version where the "" escape the characters

if ($('ul li a[href="#!..."]').length) {