Using jQuery check whether an element exists

When using jQuery, have you ever wondered how to test whether an element exists using a selector? So I thought I’d share how it’s done here because it’s not as simple as it seems. The obvious thing would simply be to wrap a selector in an if statement, right? if ($("#yourDivTag")){ // do something here } No its not .. there might be other ways of doing it, but simplest way is using the length property. See example below: if ($("#yourDivTag").length > 0){ // do something here }