clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Check if element exists in jQuery

Filed under: JavaScript— Tagged with: jquery, snippets

Simple way to check if element exists in jQuery.

The $('#module2') is always truthy, because an empty array is truthy Boolean([]). So the length needs to be checked:

if ($('#module2').length > 0) {
  // Exists
} else {
  // Does not exist
}

Comments would go here, but the commenting system isn’t ready yet, sorry.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!