What is the best way to detect if a jQuery-selector returns an empty object. If you do:
alert($(\'#notAnElement\'));
you get [object Object
My preference, and I have no idea why this isn't already in jQuery:
$.fn.orElse = function(elseFunction) {
if (!this.length) {
elseFunction();
}
};
Used like this:
$('#notAnElement').each(function () {
alert("Wrong, it is an element")
}).orElse(function() {
alert("Yup, it's not an element")
});
Or, as it looks in CoffeeScript:
$('#notAnElement').each ->
alert "Wrong, it is an element"; return
.orElse ->
alert "Yup, it's not an element"