Mike, I think you've hit the nail on the head. It isn't intuitive that
jQuery always returns an array-like object. One assumes that a failed
search returns nothing. That leads me to question why it returns the
empty array. Shouldn't it return nothing if nothing is found?

Larry


Michael Geary wrote:
> It's funny, I was going to suggest the same thing, right down to the method
> name, but Hamish beat me to it. :-)
>
> It's interesting that people don't immediately think of using
> $('#id').length > 0 (with or without the > 0). It tells me that they're not
> aware of a fundamental fact about jQuery: The $() function always returns an
> array-like object that has .length and [0..n] properties just like any Array
> - even when you use an ID selector. Once you understand that, a lot of
> things fall into place. Maybe the documentation should emphasize this point
> more.
>
> Consider the corresponding question for a string: "Does the string have any
> characters in it?"
>
> You could code:
>
> if( string.match(/.+/) ) { alert('The string has text'); }
>
> But you're more likely to write:
>
> if( string.length > 0 ) { alert('The string has text'); }
>
> Of course, every JavaScript programmer should acquire some familiarity with
> regular expressions, and should know why .match(/.+/) works - but you
> probably wouldn't use it when a simple .length test is sufficient.
>
> Anyway, Hamish's .exists() function does seem like the best of both worlds.
> A couple of little improvements: It doesn't need to be defined inside a
> $(document).ready(), and as long as we're encapsulating the logic anyway,
> it's better to use the more efficient .length>0 test. So, the code would be:
>
> jQuery.fn.exists = function() {
>     return this.length > 0;
> };
>
> There are some situations where you could use .exists() or .is('*') or
> .length>0 but there is a better alternative. One case would be where you
> want to access a DOM property directly, so you need to know that the element
> actually exists before you do that:
>
> if( $('#id').exists() )
>     alert( $('#id')[0].tagName );
>
> It would be much better to write it this way:
>
> var element = $('#id')[0];
> if( element )
>     alert( element.tagName );
>
> -Mike
>
> > From: McLars
> >
> > Hamish, I like that!
> >
> > Michael, the question is number two on the FAQ. Despite what
> > seasoned programmers may think, it's obviously not intuitive
> > to newbies.
> >
> >
> > Personally, I think it's always good to point out
> > alternatives to learners. The .is() method is actually quite
> > versatile and can be used to test for more than just
> > existence. It doesn't hurt to keep an open mind and look at
> > all the alternatives--even for something as simple as
> > checking existence. I didn't even know about the .is() method before.
> > I looked in the docs, tried it out, and actually learned something.
> >
> > Larry
> >
> >
> > On Dec 26, 3:08�pm, Hamish Campbell <[EMAIL PROTECTED]> wrote:
> > > extendify!
> > >
> > > $(document).ready(function() {
> > > � � � � jQuery.fn.exists = function() {
> > > � � � � � � � � return ( this.is('*') )
> > > � � � � � � � � }
> > > � � � � }
> > >
> > > });
> > >
> > > On Dec 27, 10:28�am, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> > >
> > >
> > >
> > > > It's funny, if I saw:
> > >
> > > > if( $('#id').is('*') ) { ... }
> > >
> > > > I would have no clue what the code was trying to do until
> > I thought
> > > > hard about it: "Let's see... is star... Now that's going
> > to match *anything*.
> > > > Wouldn't it always return true? Naw, that can't be right,
> > what would
> > > > be the point of this code... Oh! What if there are no elements at
> > > > all? Then it would return false! I think it would anyway.
> > Better check the docs. Hmm...
> > > > The docs don't explicitly say what .is() does when the
> > array is empty.
> > > > Better check the source code, and maybe try a couple of
> > test cases
> > > > to be sure."
> > >
> > > > Where if I saw:
> > >
> > > > if( $('#id').length ) { ... }
> > >
> > > > I would know right away what it does: "$() returns an
> > array. Does it
> > > > have any elements?" :-)
> > >
> > > > -Mike
> > >
> > > > > From: McLars
> > >
> > > > > $('#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.
> > > > > > From: Alexey Blinov
> > >
> > > > > > 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- Hide
> > > > > > quoted text -
> > >
> > > > - Show quoted text -- Hide quoted text -
> > >
> > > - Show quoted text -
> >

Reply via email to