Puoi usare l'attributo length per testare se un elemento esiste o meno (length = 0 significa che non esiste).
sapendo questo, puoi estendere i metodi di JQuery per comodità:
codice:
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/jquery-ui-1.8.5.custom.css"/>
<script src="scripts/jquery-1.4.2.min.js"></script>
<script src="scripts/jquery-ui-1.8.5.custom.min.js"></script>
<script>
$.fn.exists = function(){
return $(this).length > 0;
};
$(document).ready(function(){
alert("MyDiv esiste? " + $("#MyDiv").exists() + "\n" +
"asd esiste? " + $("#asd").exists());
});
</script>
</head>
<body>
<div id="MyDiv"></div>
</body>
</html>