Is there an "exists" function for jQuery?
Asked 07 September, 2021
Viewed 1.6K times
  • 57
Votes

How can I check the existence of an element in jQuery?

The current code that I have is this:

if ($(selector).length > 0) {
    // Do something
}

Is there a more elegant way to approach this? Perhaps a plugin or a function?

30 Answer