Determining whether jQuery has not found any element

Solution 1:

$('#idThatDoesnotexist').length is what you're looking for. (If it finds nothing, this will === 0.) So your conditional statement should probably be:

if($('#id').length) { /* code if found */ } else { /* code if not found */ }

You're getting an object returned from that alert because jQuery (almost) always returns the "jQuery object" when you use it, which is a wrapper for the elements jQuery's found that permits method chaining.

Solution 2:

Futuraprime is right but you can shorten your syntax by doing the following:

if ($("#id").length) {
   //at least one element was found
} else {
   //no elements found
}

Solution 3:

!$.isEmptyObject($.find('#id'))

This will return true if the element exists and false if it doesn't.