Skip to content

Commit 04036b0

Browse files
committed
Merge pull request jquery#16 from gjohnson/master
Improving the section on traversing. Moving iteration content to its own section.
2 parents 83c3e99 + 45a2aef commit 04036b0

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

content/jquery-basics/traversing.md

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,73 @@ title : "Traversing"
55
---
66
## Traversing
77

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.
99

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").
1110

12-
<div class="note" markdown="1">
13-
### Note
11+
### Parents
1412

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+
<div class="example" markdown="1">
16+
Selecting an element's direct parent
17+
18+
$('#myList').parent();
1619
</div>
20+
<div class="example" markdown="1">
21+
Selecting all the parents of an element that match a given selector
1722

23+
$('#myList').parents('div.section');
24+
</div>
1825
<div class="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
2027

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);
2630
</div>
31+
<div class="example" markdown="1">
32+
Selecting the closest parent, note that only one parent will be selected.
2733

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+
<div class="example" markdown="1">
41+
Selecting an element's direct children
2942

43+
$('#myList').children('li');
44+
</div>
3045
<div class="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
3252

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+
<div class="example" markdown="1">
56+
Selecting an element's next sibiling that matches the given selector
57+
58+
$('#myList').next('div.section');
59+
</div>
60+
<div class="example" markdown="1">
61+
Selecting an element's previous sibiling that matches the given selector
62+
63+
$('#myList').prev('div.section');
64+
</div>
65+
<div class="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+
<div class="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.
4077
</div>

0 commit comments

Comments
 (0)