1$(document).ready(function() {
2 var $myDiv = $('#DivID');
3
4 if ( $myDiv.length){
5 //you can now reuse $myDiv here, without having to select it again.
6 }
7
8
9});
1if ($('.element').length) {
2 // there is at least one element matching the selector
3}
1$(function() {
2 var $button = $(".the_button");
3 alert (isStale($button));
4 $button.remove();
5 alert (isStale($button));
6});
7
8function isStale($elem)
9{
10 return $elem.closest("body").length > 0;
11};