Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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

like image 375
halfbit Avatar asked Dec 16 '13 21:12

halfbit


1 Answers

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);
};
like image 93
A. Wolff Avatar answered Sep 22 '22 05:09

A. Wolff