Selecting Elements | jQuery Learning Center

PHOTO EMBED

Sat May 29 2021 01:01:00 GMT+0000 (Coordinated Universal Time)

Saved by @ejiwen

// Testing whether a selection contains elements.
if ( $( "div.foo" ).length ) {
    ...
}
content_copyCOPY

The best way to determine if there are any elements is to test the selection's .length property, which tells you how many elements were selected. If the answer is 0, the .length property will evaluate to false when used as a boolean value.

https://learn.jquery.com/using-jquery-core/selecting-elements/