Skip to content

Instantly share code, notes, and snippets.

@joeegan
Created April 21, 2011 16:26
Show Gist options
  • Select an option

  • Save joeegan/934916 to your computer and use it in GitHub Desktop.

Select an option

Save joeegan/934916 to your computer and use it in GitHub Desktop.
throwErrorIfJquerySelectorUndefined.js
// anonymous func to avoid polluting global namespace, and $ conflict
(function($){
// throw error for undefined selectors
var jQueryInit = $.fn.init;
$.fn.init = function(selector, context, rootjQuery) {
var result = new jQueryInit(selector, context, rootjQuery);
if (result.length === 0)
throw "jQuery cannot find the following selector: '" + selector + "'";
return result;
};
// USAGE
// if anywhere is you code you try to find a selector with jQuery that is not found, like so:
// $('undefinedselector');
// you will get an error thrown in the console
})(window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment