well as many other jQuery newbies?  Specifically, what signifigance does
jQuery's $() being like an array have, and even when you use an ID selector?

It is very significant -- in fact you could even say it is one of the most important code-saving aspects of the jQuery library.

When you do something like $(".myclass") you will get an array of the DOM nodes that match class .myclass. Now apply a jQuery method like css: $(".myclass").css("color","black"). jQuery knows to loop over the array of DOM nodes and apply that css method to all the DOM nodes in the array. This saves a ton of coding where you would have to manually loop over the nodes to achieve the same thing.

This shortcut is such a codesaver, in so many ways, that it is well worth it to always return an array-like object, and then have to use something like .length to determine if the jQuery object is empty.

-- Josh


----- Original Message ----- From: "Mike Schinkel" <[EMAIL PROTECTED]>
To: <jquery-en@googlegroups.com>
Sent: Thursday, December 27, 2007 5:23 AM
Subject: [jQuery] Re: check if an id exists



Michael Geary write:
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.

I'm relatively new to jQuery and yes, I am still struggling with that
concept.  As this point I understand the concept well enough for basic use
but not well enough for advanced use.  And I've looked around on the web
quite a bit but haven't been able to find a writeup that really explains if for me, not even one that explains as well as you just did. (I'm sure there
is a good writeup, I just haven't found it yet.)

Could I bother you to elaborate on the above in hopes to clarify for me as
well as many other jQuery newbies?  Specifically, what signifigance does
jQuery's $() being like an array have, and even when you use an ID selector? At this point I can cargo-cult jQuery(), but I don't quite grok it yet. Any
elaboration would be appreciated.

--
-Mike Schinkel
http://www.mikeschinkel.com/blogs/
http://www.welldesignedurls.org
http://atlanta-web.org



Reply via email to