unrecognized expression: #

I’ve tried to use one of my own jQuery plugins—that I know works—only to receive the following console messages without any reference to a code line number, hence no way to track down the culprit:

uncaught exception: Syntax error, unrecognized expression: #

I’m sure this has caught me out a number of times. Each time is far enough apart that I have forgotten how I debugged the issue previously. And each time I search the interwebs in vain. No one seems to have clearly explained what causes this exception!

Next time I encounter this problem, hopefully I remember—or find—this post, solution follows…

Solution

This exception is thrown by jQuery when you ask it to resolve the selector '#' without an id following. In my case it is often caused by my plugin code assuming an element has an id. If the element doesn’t have an id, the exception is thrown, for example:

...
var storedID = $(this).attr('id');
...
$('#'+storedID).doSomething();
...

When this doesn’t have an id, I’ve essentially asked jQuery to resolve the following:

...
$('#').doSomething();
...

Which jQuery doesn’t like to do, hence the exception.

HTH

Posted in

2 Responses to "unrecognized expression: #"

Leave a reply