First, there existed the DOM. This is the browser's way of understanding an HTML document once it has been parsed. The browser natively understands a certain way of accessing and interacting with the elements.
However, these are fairly limited and very verbose. They do not make it easy to write or to read.
This is why jQuery was created.
When you build a jQuery object with $()
, it creates a new object that "wraps" the native DOM object, and indeed often is used to wrap multiple DOM objects and to work on them together. There are a whole load of functions that are available and which do all the verbose leg work for you, so your code is easy both to write and to read. Nothing new is possible with jQuery, but it's a lot quicker to write and to read.
In this case, elem
is the native DOM object. When iterating over the collection with each
, jQuery doesn't automatically convert DOM objects into jQuery objects. This is for two main reasons. First, you don't always need the jQuery object. If, for instance, all you want to do is get the element's tagName
property, you can just do elem.tagName
. Second, creating a jQuery object is computationally expensive. It could slow your script down significantly.
So if you want to get a jQuery object, you need to use the jQuery constructor yourself.