How to extend the jQuery selector mechanism to warn me when a selector is not found?

Let's say I'm wrong when I try to find an element, and I make a typo, for example $('lsdkfj') . Instead of jQuery returning an empty array to me, I would like to return an error message in the console, for example, "The selector 'lsdkfj' cannot be found" . What is the best way to do this?

+2
source share
1 answer

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; }; 
+5
source

All Articles