Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#14279 closed feature (notabug)

$.debug() for selector debugging

Reported by: f.bagnardi@… 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.

Change History (3)

comment:1 Changed 6 years ago by timmywil

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.

comment:2 Changed 6 years ago by anonymous

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

comment:3 Changed 6 years ago by timmywil

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

Note: See TracTickets for help on using tickets.