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