Skip to content

Commit 8b46202

Browse files
committed
Incorporate feedback from jquery#110
1 parent 55bf68c commit 8b46202

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

content/using-jquery-core/jquery-object.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ attribution: Mike Pennisi
55
github : jugglinmike
66
---
77
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.
99
It has a collection of DOM elements, some familiar array functions, and a `length` property, after all.
1010
Actually, the jQuery object is more complicated than that.
1111

12-
### What is a DOM element, anyway?
12+
### What is a DOM element? What is the DOM, for that matter?
1313

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.
1617
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).
1819

1920
Elements have properties like any JavaScript object.
2021
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.
8687
Checking the `length` property is a common way to ensure that the selector described something on the page.
8788

8889
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.
9091

9192
<javascript caption="Selecting only the first 'h1' element on the page (in a jQuery object)">
9293
var headers = $("h1");
@@ -116,7 +117,7 @@ One such instance is making comparisons.
116117
### Not all jQuery objects are created `===`
117118

118119
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*.
120121

121122
<javascript caption="Creating two jQuery objects for the same element">
122123
var logo1 = $("#logo");
@@ -148,7 +149,7 @@ There is nothing magic about this practice--it just helps some people to keep tr
148149
We could re-write the previous example to follow this convention:
149150

150151
<javascript caption="Comparing DOM elements (with more readable variable names)">
151-
var $logo1 = $("$logo");
152+
var $logo1 = $("#logo");
152153
var logo1 = $logo1.get(0);
153154

154155
var $logo2 = $("#logo");
@@ -159,6 +160,10 @@ alert( logo1 === logo2 ); // alerts 'true'
159160

160161
This code functions identically to the example above, but it is a little more clear to read.
161162

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+
162167
### jQuery objects are not "live"
163168

164169
Given a jQuery object with all the paragraph elements on the page:
@@ -168,7 +173,9 @@ var allParagraphs = $("p");
168173
</javascript>
169174

170175
...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.
172179
The set of elements contained within a jQuery object will not change unless you modify it in your code.
173180
This means that the collection is not "live"--it does not automatically update as the document changes.
174181
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

Comments
 (0)