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
+57-20Lines changed: 57 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -5,36 +5,73 @@ 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 deeper into what was 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 string selector and some can also take another jQuery object in order to filter your selection down. Be sure to pay attention and refer to the API docs to know what all variation of arguments you have available.
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
+
The 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 that match a given selector
17
22
23
+
$('#myList').parents('div.section');
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
+
var section = $('div.section');
29
+
$('#myList').parentsUntil(section);
26
30
</div>
31
+
<divclass="example"markdown="1">
32
+
Selecting the closest parent, note that only one parent will be selected.
27
33
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.
34
+
$('#myList').closest('#navigation');
35
+
</div>
36
+
### Children
37
+
38
+
The methods for finding child 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.
39
+
40
+
<divclass="example"markdown="1">
41
+
Selecting an element's direct children
29
42
43
+
$('#myList').children('li');
44
+
</div>
30
45
<divclass="example"markdown="1">
31
-
Iterating over a selection
46
+
Finding all the links within a selection that match the selector
47
+
48
+
$('#myList').find('a.external');
49
+
</div>
50
+
51
+
### Sibilings
32
52
33
-
$('#myList li').each(function(idx, el) {
34
-
console.log(
35
-
'Element ' + idx +
36
-
'has the following html: ' +
37
-
$(el).html()
38
-
);
39
-
});
53
+
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 basic methods, similar to how `$.fn.parentsUntil` works; `$.fn.nextAll`, `$.fn.nextUntil`, `$.fn.prevAll` and `$.fn.prevUntil`.
54
+
55
+
<divclass="example"markdown="1">
56
+
Selecting an element's next sibiling that matches the given selector
57
+
58
+
$('#myList').next('div.section');
59
+
</div>
60
+
<divclass="example"markdown="1">
61
+
Selecting an element's previous sibiling that matches the given selector
62
+
63
+
$('#myList').prev('div.section');
64
+
</div>
65
+
<divclass="example"markdown="1">
66
+
Selecting an element's sibilings in both directions that matches the given selector
67
+
68
+
$('#myList').sibilings('div.section');
69
+
</div>
70
+
71
+
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")
72
+
73
+
<divclass="note"markdown="1">
74
+
### Note
75
+
76
+
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