0
# Stack Operations
1
2
Method chaining history management for complex query operations. The stack module maintains a history of element collections during chaining operations, allowing developers to navigate back to previous selections.
3
4
## Capabilities
5
6
### Chain Navigation
7
8
Methods for navigating through the chain history during complex DOM operations.
9
10
```javascript { .api }
11
/**
12
* End the most recent filtering operation and return to the previous set of elements
13
* @returns Previous collection in the chain
14
*/
15
$.fn.end();
16
17
/**
18
* Add the previous set of elements to the current collection
19
* @returns Combined collection including both current and previous elements
20
*/
21
$.fn.andSelf();
22
```
23
24
**Usage Examples:**
25
26
```javascript
27
// Using end() to navigate back in the chain
28
$('#container')
29
.find('.item')
30
.addClass('highlighted')
31
.end() // Back to #container
32
.addClass('processed');
33
34
// Complex chaining with multiple end() calls
35
$('.list')
36
.find('li')
37
.filter('.active')
38
.addClass('current')
39
.end() // Back to all li elements
40
.end() // Back to .list elements
41
.fadeIn();
42
43
// Using andSelf() to combine sets
44
$('.parent')
45
.children('.child')
46
.andSelf() // Include both .parent and .child elements
47
.addClass('combined');
48
```
49
50
### Chain History
51
52
The stack operations modify the following methods to support chaining history:
53
54
- `filter()` - Maintains reference to unfiltered collection
55
- `add()` - Maintains reference to original collection
56
- `not()` - Maintains reference to original collection
57
- `eq()` - Maintains reference to full collection
58
- `first()` - Maintains reference to full collection
59
- `last()` - Maintains reference to full collection
60
- `find()` - Maintains reference to search context
61
- `closest()` - Maintains reference to original collection
62
- `parents()` - Maintains reference to original collection
63
- `parent()` - Maintains reference to original collection
64
- `children()` - Maintains reference to parent collection
65
- `siblings()` - Maintains reference to original collection
66
67
### Advanced Chain Navigation
68
69
Complex operations combining multiple chain navigation techniques.
70
71
```javascript
72
// Multi-level navigation example
73
$('#form')
74
.find('input')
75
.filter('[required]')
76
.addClass('required-field')
77
.end() // Back to all inputs
78
.filter('[type="email"]')
79
.addClass('email-field')
80
.end() // Back to all inputs
81
.end() // Back to #form
82
.find('button')
83
.addClass('form-button');
84
85
// Combining andSelf() with filtering
86
$('.menu')
87
.find('.menu-item')
88
.filter('.active')
89
.andSelf() // Include both active items and their parent menu
90
.addClass('highlight-group');
91
92
// Working with siblings and navigation
93
$('.selected')
94
.siblings()
95
.addClass('sibling')
96
.end() // Back to .selected elements
97
.addClass('original-selection');
98
```
99
100
### Chain Stack Internal Structure
101
102
The stack maintains internal references to previous collections:
103
104
```javascript
105
// Internal structure (for reference only)
106
{
107
length: 3,
108
0: element1,
109
1: element2,
110
2: element3,
111
prevObject: previousZeptoCollection, // Used by end()
112
context: originalContext // Original search context
113
}
114
```
115
116
This internal structure allows `end()` to return to the `prevObject` and `andSelf()` to combine the current collection with `prevObject`.