jQuery how to know if div with specific id exists

To check if the div with specific id or class exist, you can use .length after the selector to see if it matched any elements, like this:

if($("#id").length == 0) {
  //it doesn't exist
}
else{
  //it exist, do something
}

By Keenlio, June 3, 2014

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *


3 − = zero

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>