What is the best way to detect if a jQuery-selector returns an empty object. If you do:
alert($(\'#notAnElement\'));
you get [object Object
I like to do something like this:
$.fn.exists = function(){
return this.length > 0 ? this : false;
}
So then you can do something like this:
var firstExistingElement =
$('#iDontExist').exists() || //<-returns false;
$('#iExist').exists() || //<-gets assigned to the variable
$('#iExistAsWell').exists(); //<-never runs
firstExistingElement.doSomething(); //<-executes on #iExist
http://jsfiddle.net/vhbSG/