Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs
Contact   •   Articles   •   Products   •   Support   •   Search
Ad-free experience sponsored by:
ASPOSE - the market leader of .NET and Java APIs for file formats – natively work with DOCX, XLSX, PPT, PDF, images and more

Understanding the jQuery Object returned from a jQuery Selector Query


:P
On this page:

Cody Lindley has a great post that explains some of the innards of the jQuery object returned from selector queries. As you may know if you are using jQuery, the jQuery object returned from a jQuery selector expression is an object that acts like an array, but is in fact an object with numeric properties that masquerade as an array. If this sounds a little confusing you’re not alone. Even though I had figured this out some time ago, every time I talk about jQuery I seem to get my terminology mixed up calling it the jQuery object and jQuery array interchangeably <snicker>... which is not correct – it’s definitely an object.

Anyway, check out Cody’s Post on the Learning jQuery site which explains in some detail how the jQuery object is constructed and how it is that you can treat an object like an array which is effectively a neat hack that might be useful to you in other JavaScript situations.

BTW the Learning jQuery site is a good site to keep bookmarked and RSS hooked since they frequently cover interesting content that explains some of the inner workings of jQuery… highly recommended.


The Voices of Reason


 

David Fauber
December 29, 2008

# re: Understanding the jQuery Object returned from a jQuery Selector Query

Hey Rick,
Just a semantic note, I think what you are referring to is actually known as the jquery wrapped element set. (the jQuery object actually being the main beast that you add utility methods,etc to)

Rick Strahl
December 29, 2008

# re: Understanding the jQuery Object returned from a jQuery Selector Query

@David - I think both are actually correct. The jQuery CLASS/FUNCTION is what you attach utility methods to. When you use selector syntax with jQuery you are actually calling the jQuery constructor which returns a jQuery object which is also known as the wrapped set of DOM objects.

Actually the only place I saw 'wrapped set' mentioned as such is in jQuery in Action and I've taken to using that myself because it clearly says what the object does. Still it's basically an object instance and that's what I was referring to when I said the jQuery object.

Simone
December 31, 2008

# re: Understanding the jQuery Object returned from a jQuery Selector Query


harvey mushman
December 31, 2008

# re: Understanding the jQuery Object returned from a jQuery Selector Query

Understanding jQuery is one thing ...but understanding jQuery on a ski trip is another thing!<g>

Rick Strahl
January 01, 2009

# re: Understanding the jQuery Object returned from a jQuery Selector Query

@Simone - your post really discusses some different aspects of the jQuery object. Agree though: the fact that it's always returning a jQuery object even if not match is found can be confusing at first. This actually makes good sense in the big picture and is done so that chaining still works even if no matched set was found. Without this feature it'd be a lot easier to break code and you wouldn't be able to chain commands as you'd have to check for no matches along the way (shades of XMLDOM work come to mind - jQuery makes that much smoother).

groovin101
July 13, 2010

# re: Understanding the jQuery Object returned from a jQuery Selector Query


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2020