jQuery If Element Exists
1 min read

jQuery If Element Exists

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
}