How to Check if Element Exists With jQuery

In order to test if an element exists or not we are going to use jQuery length() method. if the element exists jQuery property will return total number of matched elements on the page.

Quick Tip : How to get element by href in jQuery?

$(document).ready(function(){  
   if ($('img').length) {   
      console.log('We have found at least one image on the page');  
   } else {  
      console.log('There is no image on the page');  
   }  
  
});

 

0 Comments

Leave a Comment.