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?
javascriptjquery
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?
Best Solution
In JavaScript, everything is 'truthy' or 'falsy', and for numbers
0meansfalse, everything elsetrue. So you could write:You don't need that
>0part.