You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/using-jquery-core/jquery-object.md
+16-9Lines changed: 16 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -5,16 +5,17 @@ attribution: Mike Pennisi
5
5
github : jugglinmike
6
6
---
7
7
Presumably, if you're reading this, you are curious about the thing that jQuery returns when you create new elements (or select existing ones).
8
-
This curiosity is a great first step, because a first glance, the jQuery object may seem to be an array of DOM elements.
8
+
This curiosity is a great first step, because at first glance, the jQuery object may seem to be an array of DOM elements.
9
9
It has a collection of DOM elements, some familiar array functions, and a `length` property, after all.
10
10
Actually, the jQuery object is more complicated than that.
11
11
12
-
### What is a DOM element, anyway?
12
+
### What is a DOM element? What is the DOM, for that matter?
13
13
14
-
A DOM element is a "piece" of a web page.
15
-
It can contain text and/or other DOM elements.
14
+
The DOM (short for Document Object Model) is a representation of an HTML document.
15
+
It may contain any number of DOM elements.
16
+
At a high level, you can think a DOM element as a "piece" of a web page. It can contain text and/or other DOM elements.
16
17
It is described by a type (i.e. "div", "a", "p", etc.) and any number of attributes (i.e. "src", "href", "class", etc.).
17
-
For a more thorough description, please refer to [the office specification from the W3C](http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-745549614).
18
+
For a more thorough description, please refer to [the official specification from the W3C](http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-745549614).
18
19
19
20
Elements have properties like any JavaScript object.
20
21
Among these properties are attributes like `tagName` and methods like `appendChild`.
@@ -86,7 +87,7 @@ Likewise, if the page has no `<h1>` tags, the `length` property will be zero.
86
87
Checking the `length` property is a common way to ensure that the selector described something on the page.
87
88
88
89
To make sure we only have the first header element, we have to take one more step.
89
-
There are a number of ways to accomplish that, the most straight-forward may be the `eq()` function.
90
+
There are a number of ways to accomplish this, the most straight-forward may be the `eq()` function.
90
91
91
92
<javascriptcaption="Selecting only the first 'h1' element on the page (in a jQuery object)">
92
93
var headers = $("h1");
@@ -116,7 +117,7 @@ One such instance is making comparisons.
116
117
### Not all jQuery objects are created `===`
117
118
118
119
An important detail regarding this "wrapping" behavior is that each wrapped object is unique.
119
-
This is true *even if the object was created with the same selector*.
120
+
This is true *even if the object was created with the same selector or contain references to the exact same DOM elements*.
120
121
121
122
<javascriptcaption="Creating two jQuery objects for the same element">
122
123
var logo1 = $("#logo");
@@ -148,7 +149,7 @@ There is nothing magic about this practice--it just helps some people to keep tr
148
149
We could re-write the previous example to follow this convention:
149
150
150
151
<javascriptcaption="Comparing DOM elements (with more readable variable names)">
This code functions identically to the example above, but it is a little more clear to read.
161
162
163
+
However you choose to organize your code, it is very important to make the distinction between jQuery object and native DOM elements!
164
+
You cannot access native DOM methods and properties directly from a jQuery object, and vice versa.
165
+
**If you see error messages like, "event.target.closest is not a function"' or "TypeError: Object [object Object] has no method 'setAttribute'" you are likely making this common mistake and now have the knowledge to remedy it.**
166
+
162
167
### jQuery objects are not "live"
163
168
164
169
Given a jQuery object with all the paragraph elements on the page:
@@ -168,7 +173,9 @@ var allParagraphs = $("p");
168
173
</javascript>
169
174
170
175
...you might expect that the contents will grow and shrink over time as `<p>` elements are added and removed from the document.
171
-
This is *not* actually the case.
176
+
This is how "nodelists" returned by the `getElementsByTagName` method work, after all.
177
+
178
+
jQuery objects do **not** behave in this manner.
172
179
The set of elements contained within a jQuery object will not change unless you modify it in your code.
173
180
This means that the collection is not "live"--it does not automatically update as the document changes.
174
181
If you expect the document may have changed from when you created the jQuery object, then update your collection by creating a new one!
0 commit comments