Skip to content

Commit 8263c12

Browse files
committed
new traversing docs
1 parent ae5629a commit 8263c12

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

content/jquery-basics/traversing.md

+52-20
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,68 @@ 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 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.
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+
There are four 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 which match a given selector
1722

23+
$('#myList').parents('div');
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+
$('#myList').parentsUntil('div');
2629
</div>
2730

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
2932

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+
<div class="example" markdown="1">
36+
Selecting an element's direct children
37+
38+
$('#myList').children();
39+
</div>
3040
<div class="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`.
3249

33-
$('#myList li').each(function(idx, el) {
34-
console.log(
35-
'Element ' + idx +
36-
'has the following html: ' +
37-
$(el).html()
38-
);
39-
});
50+
<div class="example" markdown="1">
51+
Selecting an element's next sibiling that matches the given selector
52+
53+
$('#myList').next('div.section');
54+
</div>
55+
<div class="example" markdown="1">
56+
Selecting an element's previous sibiling that matches the given selector
57+
58+
$('#myList').prev('div.section');
59+
</div>
60+
<div class="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+
<div class="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.
4072
</div>

0 commit comments

Comments
 (0)