Skip to content

Instantly share code, notes, and snippets.

@devinrhode2
Last active November 27, 2018 17:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinrhode2/4575621 to your computer and use it in GitHub Desktop.
Save devinrhode2/4575621 to your computer and use it in GitHub Desktop.
warn on empty jquery selection badSelectorAction devinrhode2

warn on empty jquery selection

Graduated to github: https://github.com/devinrhode2/StrictjQuery.js

But for the simple version (quite possibly better): (wait no its broke use the one on GH)

Warns when you have an empty jQuery selection, unless you do $('foo', failsafe);

  $.badSelectorAction = function badSelectorActionFn( selector, context ) {
    console.warn('Empty selection for selector:', selector, 'context:', context);
  };
  
  window.failsafe = 'failsafe';
  
  jQuery.fn.oldInit = jQuery.fn.init;
  var rootjQuery = jQuery(document);
  jQuery.fn.init = function strictSelectorOverride( selector, context ) {
    var result = jQuery.fn.oldInit.call(this, selector, context, rootjQuery);
    if (typeof selector === 'string') {
      if (result || context === failsafe) {
        return result;
      } else {
        $.badSelectorError( selector, context );
      }
    } else {
      return result;
    }
  };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment