xxxxxxxxxx
if ($('.element').length) {
// there is at least one element matching the selector
}
xxxxxxxxxx
if ($('#elementId').length) {
// Element exists
} else {
// Element does not exist
}
xxxxxxxxxx
$(function() {
var $button = $(".the_button");
alert (isStale($button));
$button.remove();
alert (isStale($button));
});
function isStale($elem)
{
return $elem.closest("body").length > 0;
};