<p>How can I check the existence of an element in jQuery?</p>

<p>The current code that I have is this:</p>

<pre><code>if ($(selector).length &gt; 0) {
    // Do something
}
</code></pre>

<p>Is there a more elegant way to approach this? Perhaps a plugin or a function?</p>

Yes!
```
```
jQuery.fn.exists = function(){ return this.length > 0; }

if ($(selector).exists()) {
    // Do something
}
```
```
This is in response to:
Herding Code podcast with Jeff Atwood