5

While testing my website in Firebug i get this error when clicking on a menu button:

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

I think it goes wrong here because the current class won't apply but the rest works fine.(these aren't the full 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");
4
  • 1
    There is something wrong with your code. Unless you show the code, the only answers that you can get will be based on people guessing what your code might look like. Commented Apr 28, 2011 at 12:30
  • 1
    While I could take a guess … I'm not going to. If your code is throwing errors, then don't provide just the error. It is hard to identify problems in secret code we cannot see. Commented Apr 28, 2011 at 12:30
  • 1
    Firebug is already shown what is the wrong. Commented Apr 28, 2011 at 12:31
  • 1
    My guess: you forgot to wrap your selector in quotes: $([href=schedule.html])... Commented Apr 28, 2011 at 12:34

2 Answers 2

8

This looks like your culprit:

// add single quotes on your selector value 
$("nav a[href='"+newHash+"']").addClass("current");
Sign up to request clarification or add additional context in comments.

Comments

3

Since jquery 1.5, quoting attribute values is mandatory. You can quote with single or double quotes:

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

or

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

Quoting was optional in jQuery 1.4 or lower.

1 Comment

The course I'm taking (Lynda.com -> jQuery Essential Training) uses a jQuery version that doesn't require these quotes yet. So in the video you see all is working fine without them but if you use a newer jQuery version, you do need them!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.