jQuery check dom element exists in page

Here I am going to explain how to check or validate DOM element exists in a page or not.

we used to check DOM element existence like jQuery(‘#ID’) or jQuery(‘.Class’) but keep in mind jQuery always return array weather DOM element exists or not. If not exists then it returns blank array so we must check array length to know is array has element or not.

Syntax:

// wrong way
if(jQuery(‘#id’)){}

// Correct way
if(jQuery(‘#id’).length){}

Be the first to comment on "jQuery check dom element exists in page"

Leave a comment

Your email address will not be published.


*