jquery if element exists code example

Example 1: CHECK IF element is hidden jquery

// Checks CSS content for display:[none|block], ignores visibility:[true|false]
$(element).is(":visible");

// The same works with hidden
$(element).is(":hidden");

Example 2: jquery check if element exists

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

}

Example 3: jquery test div exists

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

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


});

Example 4: jquery check if attribute exists

var attr = $(this).attr('name');

// For some browsers, `attr` is undefined; for others,
// `attr` is false.  Check for both.
if (typeof attr !== typeof undefined && attr !== false) {
    // ...
}

Example 5: check element exist in jquery

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

Example 6: jquery check if exist

if ( $( "#myDiv" ).length ) {
 
    $( "#myDiv" ).show();
 
}