I would definitely recommend $("#id").length. When working in a
language, it's important to keep in mind the language's idioms. In the
case of JS, 0 == false, so if($("#id").length) is a perfectly good
idiom.
It's a bit confusing coming from another language (where 0 is true),
but that's not a good enough reason to reduce the efficiency of your
code.
-- Yehuda
On Dec 25, 2007, at 7:35 PM, McLars wrote:
$('#id').length is the old school, and most widely used, technique.
This is probably the fastest.
$('#id').is('*') does make sense semantically (expresses the intent),
and is more flexible.
Larry
On Dec 25, 12:33 am, "Alexey Blinov" <[EMAIL PROTECTED]> wrote:
Yep... my code have size(). Forgot to point it...
And thanks for info about more efficient way - using length.
So... which way is better than?
1. $('#id').length > 0
2. $('#id').length() !== 0
3. $('#id').is('*') //never try it... but look pretty
- Alexey
On Dec 25, 2007 5:39 AM, Michael Geary <[EMAIL PROTECTED]> wrote:
$('#id').size !== 0 would not work. I'll bet that your actual
code has
.size() instead of .size without the parentheses, right? The size
property
is a method, so the !== 0 test would always return true (since a
function
reference is never equal to 0).
.length is slightly more efficient than .size() - look at the
source code
for the .size method:
size: function() {
return this.length;
}
The only reason the size method exists at all is for compatibility
with
very old jQuery code that may have used it back in the days when
there was
no .length property.
-Mike- Hide quoted text -
- Show quoted text -