Say I make a mistake when I'm trying to find an element and I make a typo, like $('lsdkfj').
Instead of jQuery returning me an empty array, I'd like to return an error message in the console, like
"The selector 'lsdkfj' cannot be found". What is the best way to go about doing this?
Asked
Active
Viewed 665 times
2
Dr. Frankenstein
- 4,634
- 7
- 33
- 48
-
Download an un-minified version of jQuery and start placing `console.info` calls. – user229044 Apr 21 '11 at 13:52
-
3Use the source Luke: https://github.com/jquery/jquery/blob/master/src/core.js#L72 – Robert Apr 21 '11 at 14:00
1 Answers
5
Like this:
var oldInit = $.fn.init;
$.fn.init = function(selector, context, rootjQuery) {
var result = new oldInit(selector, context, rootjQuery);
if (result.length === 0)
console.info("jQuery call has no elements!", arguments);
return result;
};
SLaks
- 868,454
- 176
- 1,908
- 1,964
-
This would actually throw an error, since the context wouldn't be correct for `makeArray()`. – Nick Craver Apr 21 '11 at 14:20
-
-
Fixed. I'm still not sure what the problem was; `this` should have been the new object since `init` is called as a ctor. – SLaks Apr 21 '11 at 14:52
-
@SLaks - Make sure to test!, this will call `console.info()` every time, since the length will be `0` on the first pass :) – Nick Craver Apr 21 '11 at 14:56
-