jquery check if element exists
snippet in javascript

jquery check if element exists

user6543

// Check if an element currently exists
if ($('#element').length) {

}

jquery test div exists

user8914

$(document).ready(function() {
    var $myDiv = $('#DivID');

    if ( $myDiv.length){
        //you can now reuse  $myDiv here, without having to select it again.
    }


});

check element exist in jquery

user8100

if ($('.element').length) {
  // there is at least one element matching the selector
}

Is there an “exists” function for jQuery

user4451

if ($(selector).length)

jquery check if element still exists

user3952

$(function() {
    var $button = $(".the_button");
    alert (isStale($button));
    $button.remove();
    alert (isStale($button));
});
    
function isStale($elem)
{
    return $elem.closest("body").length > 0;
};

jQuery exists function

user3986

if ($("#myElementID").length){
   //I exist
}