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/jquery-basics/traversing.md
+52-20
Original file line number
Diff line number
Diff line change
@@ -5,36 +5,68 @@ title : "Traversing"
5
5
---
6
6
## Traversing
7
7
8
-
Once you have a jQuery selection, you can find other elements using your selection as a starting point.
8
+
Once you have made an intial selection with jQuery, you may want to traverse a little deeper into what you just selected. Traversing can be broken down into three basic parts: parents, children and sibilings. jQuery has an abundance of easy to use methods for all these parts. You will notice that each of these methods can optionaly be passed another selector or selection in order to refine your selection even more.
9
9
10
-
For complete documentation of jQuery traversal methods, visit [http://api.jquery.com/category/traversing/](http://api.jquery.com/category/traversing/"Traversing documentation on api.jquery.com").
11
10
12
-
<divclass="note"markdown="1">
13
-
### Note
11
+
### Parents
14
12
15
-
Be cautious with traversing long distances in your documents — complex traversal makes it imperative that your document's structure remain the same, something that's difficult to guarantee even if you're the one creating the whole application from server to client. One- or two-step traversal is fine, but you generally want to avoid traversals that take you from one container to another.
13
+
There are four methods for finding the parents from a selection: `$.fn.parent`, `$.fn.parents`, `$.fn.parentsUntil` and `$.fn.closest`.
14
+
15
+
<divclass="example"markdown="1">
16
+
Selecting an element's direct parent
17
+
18
+
$('#myList').parent();
16
19
</div>
20
+
<divclass="example"markdown="1">
21
+
Selecting all the parents of an element which match a given selector
17
22
23
+
$('#myList').parents('div');
24
+
</div>
18
25
<divclass="example"markdown="1">
19
-
Moving around the DOM using traversal methods
26
+
Selecting all the parents of an element up to, but *not including* the selector
20
27
21
-
$('h1').next('p');
22
-
$('div:visible').parent();
23
-
$('input[name=first_name]').closest('form');
24
-
$('#myList').children();
25
-
$('li.selected').siblings();
28
+
$('#myList').parentsUntil('div');
26
29
</div>
27
30
28
-
You can also iterate over a selection using `$.fn.each`. This method iterates over all of the elements in a selection, and runs a function for each one. The function receives the index of the current element and the DOM element itself as arguments. Inside the function, the DOM element is also available as `this` by default.
31
+
### Children
29
32
33
+
There are only 2 methods for finding children elements from a selection: `$.fn.children` and `$.fn.find`. The difference between these methods lies in how far into the child structure the selection is made. `$.fn.children` only operates on direct child nodes, while `$.fn.find` can traverse recursively into children, and children of those children, etc.
34
+
35
+
<divclass="example"markdown="1">
36
+
Selecting an element's direct children
37
+
38
+
$('#myList').children();
39
+
</div>
30
40
<divclass="example"markdown="1">
31
-
Iterating over a selection
41
+
Finding all the links within a selection that match the selector
42
+
43
+
$('#myList').find('a.external');
44
+
</div>
45
+
46
+
### Sibilings
47
+
48
+
The rest of the traversal methods within jQuery all deal with finding sibiling selections. There are a few basic methods as far as direction is concerned. You can find previous elements with `$.fn.prev`, next elements with `$.fn.next` and both with `$.fn.sibilings`. There are also a few other methods that build onto these methods, similar to how `$.fn.parentsUntil` works; `$.fn.nextAll`, `$.fn.nextUntil`, `$.fn.prevAll` and `$.fn.prevUntil`.
32
49
33
-
$('#myList li').each(function(idx, el) {
34
-
console.log(
35
-
'Element ' + idx +
36
-
'has the following html: ' +
37
-
$(el).html()
38
-
);
39
-
});
50
+
<divclass="example"markdown="1">
51
+
Selecting an element's next sibiling that matches the given selector
52
+
53
+
$('#myList').next('div.section');
54
+
</div>
55
+
<divclass="example"markdown="1">
56
+
Selecting an element's previous sibiling that matches the given selector
57
+
58
+
$('#myList').prev('div.section');
59
+
</div>
60
+
<divclass="example"markdown="1">
61
+
Selecting an element's sibilings in both directions that matches the given selector
62
+
63
+
$('#myList').sibilings('div.section');
64
+
</div>
65
+
66
+
You can see all these methods metioned and more at the docs [http://api.jquery.com/category/traversing/tree-traversal/](http://api.jquery.com/category/traversing/tree-traversal/"Traversal documentation on api.jquery.com")
67
+
68
+
<divclass="note"markdown="1">
69
+
### Note
70
+
71
+
Be cautious with traversing long distances in your documents — complex traversal makes it imperative that your document's structure remain the same, something that's difficult to guarantee even if you're the one creating the whole application from server to client. One- or two-step traversal is fine, but you generally want to avoid traversals that take you from one container to another.
0 commit comments