jQuery If Element Exists

August 30, 2012 — Leave a comment

Three different ways to check if an element exists using a selector in jQuery.

if ($("#mydiv").length > 0){
  // do something here
}

if ($("#mydiv").length){
  // do something here
}

if ($("#mydiv")[0]) {
  // do something
}

Mike

Posts Twitter Facebook

My name is Michael Le, and I am a web developer.

No Comments

Be the first to start the conversation.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.