I just spent \'hours\' with the following scenario: (all back to the basics)
$(\'#typo\').bind(\'click\' etc ...
I had a typo in the selec
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);
};
The jquery selector returns an array of DOM Elements so if you check the length of it, if it is greater than 0 it found something, if it is equal to 0 it didn't
if($("selector").length > 0) found;
You can use the length of the returned DOM elements as a falsy and throw a new error
like so:
var el = '#el';
if ($(el).length){
// Do Stuff
} else {
throw new Error("Element "+el+" Doesn't Exist");
}
OR
// Ignores Empty Selector
if ($('#el').length){
// Do Stuff
}
I hope this helps!
var element = $('#typo');
if (element.length > 0) {
// element exists
}
Short of that, you would have to modify the jQuery source code to get that behavior.