Is there a 'not found' exception for jquery selector?

jquery

Solution

You could use following snippet:

UPDATED to take care of context

DEMO

jQuery.debug = true;
$ = function (selector, context) {
    if (jQuery.debug && typeof selector === "string" && !jQuery(selector, context).length) 
        throw new Error("No element found!");
    return jQuery.apply(this, arguments);
};

Problem

I just spent 'hours' with the following scenario: (all back to the basics) ``` $('#typo').bind('click' etc ... ``` I had a typo in the selector, but jQuery resolves ``` $('#funny something out there').bind(... ``` without any warning. Is it possible to tell jQuery to raise an error when your selector gets nothing? Something like this: ``` $('#typo', alert('Stupid, nothing here! like '+ '#typo)) ``` edit I spoke against me: ``` $('#typo', alert('Stupid, nothing here! like '+ '#typo)) ``` is not a solutiuon. I have to know where the error is to extend the selector

Original source