I understand that the way to see if there are any elements on the page
with the class of "foo" is this: $('.foo')

On a page with such elements, I will get a result like: jQuery(p.foo,
p.foo) and if there are no matching elements, I will get jQuery() --
or at least that's how it's displayed in Firebug.

Anyhow, an empty jQuery() is not false-y even when it is empty, and
this code (below) will always result in the first alert being called,
never the second:

if ( $('.foo') ) {
  alert('at least one matching element');
} else {
  alert('nothing matches');
}

Instead, I do it like this (since zero is false-y in JavaScript):

if ( $('.foo').length ) {
  alert('at least one matching element');
} else {
  alert('nothing matches');
}

But is that the preferred way of doing this?

Thanks,
Joe

Reply via email to