if ( $("#mydiv").length ){ // do something here }
Briefer again…
if ( $("#mydiv")[0] ){ // do something here }
via http://aaronrussell.co.uk/legacy/check-if-an-element-exists-using-jquery/
if ( $("#mydiv").length ){ // do something here }
Briefer again…
if ( $("#mydiv")[0] ){ // do something here }
via http://aaronrussell.co.uk/legacy/check-if-an-element-exists-using-jquery/
I personally find this to be the best:
length is better since it returns an
(0 if no elements found). The other approach returns undefined if no elements found.
Sure, length is probably best.
But the second method works. Even when it returns undefined, it fails the conditional statement.