peirix peirix - 7 months ago 34
jQuery Question

How can I detect if a selector returns null?

What is the best way to detect if a jQuery-selector returns an empty object.
If you do:


you get [object Object], so the way I do it now is:


which will write "undefined", and so you can do a check for that. But it seems very bad. What other way is there?


My favourite is to extend jQuery with this tiny convenience:

$.fn.exists = function () {
    return this.length !== 0;

Used like:


More explicit than using length.