Caught exception: syntax error, unrecognized expression: [href = example.html]

When testing my site in Firebug, I get this error when I click on the menu button:

uncaught exception: Syntax error, unrecognized expression: [href = schedule.html]

I think this is going wrong here because the current class will not apply, but everything else works fine (this is not complete code)

HTML:

<nav>
  <ul>
    <li><a class="current" href="index.html">HOME</a></li>
    <li><a href="schedule.html">SCHEDULE</a></li>
  </ul>
</nav>

JS:

$("nav a").removeClass("current");
$("nav a[href="+newHash+"]").addClass("current");
+5
source share
2 answers

It looks like your culprit:

// add single quotes on your selector value 
$("nav a[href='"+newHash+"']").addClass("current");
+7
source

Since jquery 1.5, quoting attribute values โ€‹โ€‹is required. You can quote single or double quotes:

$("nav a[href='"+newHash+"']").addClass("current");

or

$('nav a[href="'+newHash+'"]').addClass("current");

The quote was optional in jQuery 1.4 or lower.

+3

All Articles