check if element exists jquery code example
Example 1: jquery check if element exists
if ($('#element').length) {
}
Example 2: jquery test div exists
$(document).ready(function() {
var $myDiv = $('#DivID');
if ( $myDiv.length){
}
});
Example 3: check element exist in jquery
if ($('.element').length) {
}
Example 4: jquery check if exist
if ( $( "#myDiv" ).length ) {
$( "#myDiv" ).show();
}
Example 5: jquery check if element still exists
$(function() {
var $button = $(".the_button");
alert (isStale($button));
$button.remove();
alert (isStale($button));
});
function isStale($elem)
{
return $elem.closest("body").length > 0;
};
Example 6: jQuery exists function
if ($("#myElementID").length){
}