Skip to main content

Bug Tracker

Side navigation

#14279 closed feature (notabug)

Opened August 21, 2013 08:32AM UTC

Closed August 21, 2013 02:59PM UTC

Last modified August 22, 2013 03:38PM UTC

$.debug() for selector debugging

Reported by: f.bagnardi@gmail.com Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:
Description

jQuery is great until you make a typo in a selector. When developing, I want to be able to have console warnings for selectors that match no elements.

When you call $.debug() it could decorate an internal function that's used in the selection process. Pseudocode:

$.debug = function(){
    var original = $.someFunction;
    $.someFunction = function(selector){
        var ret = original.apply(this, arguments);
        if (ret == null) {
            console.warn(selector + " matches 0 elements";
        }
        return ret;
    }
}

(Sorry, I'm not very familiar with jQuery internals.)

A more complicated debug function would allow enabling/disabling it, so you could do something like:

$.debug(true);
$(".someclass").find(".somechild")
$.debug(false);

It could also optionally be set differently in the production build.

$.debug = $.noop

If this isn't likely to be implemented, I would appreciate the name of a good function to replace for this, so I can build my own development version.

Attachments (0)
Change History (3)

Changed August 21, 2013 02:59PM UTC by timmywil comment:1

resolution: → notabug
status: newclosed

I'm not aware of a function that does that, but you could duck punch jQuery.find and send a message to the console whenever results.length is 0. This is not something we'll be adding to core though.

Changed August 21, 2013 04:17PM UTC by anonymous comment:2

Thanks. So find is called even when you do $('something')?

Changed August 22, 2013 03:38PM UTC by timmywil comment:3

Not jQuery.fn.find, but jQuery.find is called for all selectors except simple ID selectors (for which there is a fast path in $() ).