Methods for traversing the DOM structure.
Methods
# (static) add(other, contextopt) → {Cheerio}
Add elements to the set of matched elements.
Name | Type | Attributes | Description |
---|---|---|---|
other |
string | Cheerio | Elements to add. |
|
context |
Cheerio |
<optional> |
Optionally the context of the new selection. |
- Source:
The combined set.
- Type
- Cheerio
$('.apple').add('.orange').length;
//=> 2
# (static) addBack(selector) → {Cheerio}
Add the previous set of elements on the stack to the current set, optionally filtered by a selector.
Name | Type | Description |
---|---|---|
selector |
string | Selector for the elements to add. |
- Source:
The combined set.
- Type
- Cheerio
$('li').eq(0).addBack('.orange').length;
//=> 2
# (static) children(selectoropt) → {Cheerio}
Gets the children of the first selected element.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for children. |
- Source:
The children.
- Type
- Cheerio
$('#fruits').children().length;
//=> 3
$('#fruits').children('.pear').text();
//=> Pear
# (static) closest(selectoropt) → {Cheerio}
For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
Selector for the element to find. |
- Source:
The closest nodes.
- Type
- Cheerio
$('.orange').closest();
// => []
$('.orange').closest('.apple');
// => []
$('.orange').closest('li');
// => [<li class="orange">Orange</li>]
$('.orange').closest('#fruits');
// => [<ul id="fruits"> ... </ul>]
# (static) contents() → {Cheerio}
Gets the children of each element in the set of matched elements, including text and comment nodes.
- Source:
The children.
- Type
- Cheerio
$('#fruits').contents().length;
//=> 3
# (static) each(fn) → {Cheerio}
Iterates over a cheerio object, executing a function for each matched
element. When the callback is fired, the function is fired in the context of
the DOM element, so this
refers to the current element, which is equivalent
to the function parameter element
. To break out of the each
loop early,
return with false
.
Name | Type | Description |
---|---|---|
fn |
function | Function to execute. |
- Source:
The instance itself, useful for chaining.
- Type
- Cheerio
const fruits = [];
$('li').each(function (i, elem) {
fruits[i] = $(this).text();
});
fruits.join(', ');
//=> Apple, Orange, Pear
# (static) end() → {Cheerio}
End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.
- Source:
The previous state of the set of matched elements.
- Type
- Cheerio
$('li').eq(0).end().length;
//=> 3
# (static) eq(i) → {Cheerio}
Reduce the set of matched elements to the one at the specified index. Use
.eq(-i)
to count backwards from the last selected element.
Name | Type | Description |
---|---|---|
i |
number | Index of the element to select. |
- Source:
The element at the i
th position.
- Type
- Cheerio
$('li').eq(0).text();
//=> Apple
$('li').eq(-1).text();
//=> Pear
# (static) filter(match, containeropt) → {Cheerio}
Iterates over a cheerio object, reducing the set of selector elements to
those that match the selector or pass the function's test. When a Cheerio
selection is specified, return only the elements contained in that selection.
When an element is specified, return only that element (if it is contained in
the original selection). If using the function method, the function is
executed in the context of the selected element, so this
refers to the
current element.
Name | Type | Attributes | Description |
---|---|---|---|
match |
string | function | Value to look for, following the rules above. |
|
container |
Cheerio |
<optional> |
Optional node to filter instead. |
- Source:
The filtered collection.
- Type
- Cheerio
Selector
$('li').filter('.orange').attr('class');
//=> orange
Function
$('li')
.filter(function (i, el) {
// this === el
return $(this).attr('class') === 'orange';
})
.attr('class');
//=> orange
# (static) find(selectorOrHaystack) → {Cheerio}
Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.
Name | Type | Description |
---|---|---|
selectorOrHaystack |
string | Cheerio | Node | Element to look for. |
- Source:
The found elements.
- Type
- Cheerio
$('#fruits').find('li').length;
//=> 3
$('#fruits').find($('.apple')).length;
//=> 1
# (static) first() → {Cheerio}
Will select the first element of a cheerio object.
- Source:
The first element.
- Type
- Cheerio
$('#fruits').children().first().text();
//=> Apple
# (static) get(iopt) → {Node}
Retrieve the DOM elements matched by the Cheerio object. If an index is specified, retrieve one of the elements matched by the Cheerio object.
Name | Type | Attributes | Description |
---|---|---|---|
i |
number |
<optional> |
Element to retrieve. |
- Source:
The node at the i
th position.
- Type
- Node
$('li').get(0).tagName
//=> li
If no index is specified, retrieve all elements matched by the Cheerio object:
$('li').get().length;
//=> 3
# (static) has(selectorOrHaystack) → {Cheerio}
Filters the set of matched elements to only those which have the given DOM
element as a descendant or which have a descendant that matches the given
selector. Equivalent to .filter(':has(selector)')
.
Name | Type | Description |
---|---|---|
selectorOrHaystack |
string | Cheerio | Node | Element to look for. |
- Source:
The filtered collection.
- Type
- Cheerio
Selector
$('ul').has('.pear').attr('id');
//=> fruits
Element
$('ul').has($('.pear')[0]).attr('id');
//=> fruits
# (static) index(selectorOrNeedleopt) → {number}
Search for a given element from among the matched elements.
Name | Type | Attributes | Description |
---|---|---|---|
selectorOrNeedle |
string | Cheerio | Node |
<optional> |
Element to look for. |
- Source:
The index of the element.
- Type
- number
$('.pear').index();
//=> 2
$('.orange').index('li');
//=> 1
$('.apple').index($('#fruit, li'));
//=> 1
# (static) last() → {Cheerio}
Will select the last element of a cheerio object.
- Source:
The last element.
- Type
- Cheerio
$('#fruits').children().last().text();
//=> Pear
# (static) map(fn) → {Cheerio}
Pass each element in the current matched set through a function, producing a new Cheerio object containing the return values. The function can return an individual data item or an array of data items to be inserted into the resulting set. If an array is returned, the elements inside the array are inserted into the set. If the function returns null or undefined, no element will be inserted.
Name | Type | Description |
---|---|---|
fn |
function | Function to execute. |
- Source:
The mapped elements, wrapped in a Cheerio collection.
- Type
- Cheerio
$('li')
.map(function (i, el) {
// this === el
return $(this).text();
})
.get()
.join(' ');
//=> "apple orange pear"
# (static) next(selectoropt) → {Cheerio}
Gets the next sibling of the first selected element, optionally filtered by a selector.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for sibling. |
- Source:
The next nodes.
- Type
- Cheerio
$('.apple').next().hasClass('orange');
//=> true
# (static) nextAll(selectoropt) → {Cheerio}
Gets all the following siblings of the first selected element, optionally filtered by a selector.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for siblings. |
- Source:
The next nodes.
- Type
- Cheerio
$('.apple').nextAll();
//=> [<li class="orange">Orange</li>, <li class="pear">Pear</li>]
$('.apple').nextAll('.orange');
//=> [<li class="orange">Orange</li>]
# (static) nextUntil(selector, filterSelectoropt) → {Cheerio}
Gets all the following siblings up to but not including the element matched by the selector, optionally filtered by another selector.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string | Cheerio | Node | Selector for element to stop at. |
|
filterSelector |
string |
<optional> |
If specified filter for siblings. |
- Source:
The next nodes.
- Type
- Cheerio
$('.apple').nextUntil('.pear');
//=> [<li class="orange">Orange</li>]
# (static) not(match, containeropt) → {Cheerio}
Remove elements from the set of matched elements. Given a jQuery object that
represents a set of DOM elements, the .not()
method constructs a new jQuery
object from a subset of the matching elements. The supplied selector is
tested against each element; the elements that don't match the selector will
be included in the result. The .not()
method can take a function as its
argument in the same way that .filter()
does. Elements for which the
function returns true are excluded from the filtered set; all other elements
are included.
Name | Type | Attributes | Description |
---|---|---|---|
match |
string | function | Value to look for, following the rules above. |
|
container |
Array.<Node> | Cheerio |
<optional> |
Optional node to filter instead. |
- Source:
The filtered collection.
- Type
- Cheerio
Selector
$('li').not('.apple').length;
//=> 2
Function
$('li').not(function (i, el) {
// this === el
return $(this).attr('class') === 'orange';
}).length;
//=> 2
# (static) parent(selectoropt) → {Cheerio}
Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for parent. |
- Source:
The parents.
- Type
- Cheerio
$('.pear').parent().attr('id');
//=> fruits
# (static) parents(selectoropt) → {Cheerio}
Get a set of parents filtered by selector
of each element in the current
set of match elements.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for parents. |
- Source:
The parents.
- Type
- Cheerio
$('.orange').parents().length;
// => 2
$('.orange').parents('#fruits').length;
// => 1
# (static) parentsUntil(selector, filteropt) → {Cheerio}
Get the ancestors of each element in the current set of matched elements, up to but not including the element matched by the selector, DOM node, or cheerio object.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string | Node | Cheerio | Selector for element to stop at. |
|
filter |
string | function |
<optional> |
Optional filter for parents. |
- Source:
The parents.
- Type
- Cheerio
$('.orange').parentsUntil('#food').length;
// => 1
# (static) prev(selectoropt) → {Cheerio}
Gets the previous sibling of the first selected element optionally filtered by a selector.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for siblings. |
- Source:
The previous nodes.
- Type
- Cheerio
$('.orange').prev().hasClass('apple');
//=> true
# (static) prevAll(selectoropt) → {Cheerio}
Gets all the preceding siblings of the first selected element, optionally filtered by a selector.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for siblings. |
- Source:
The previous nodes.
- Type
- Cheerio
$('.pear').prevAll();
//=> [<li class="orange">Orange</li>, <li class="apple">Apple</li>]
$('.pear').prevAll('.orange');
//=> [<li class="orange">Orange</li>]
# (static) prevUntil(selector, filterSelectoropt) → {Cheerio}
Gets all the preceding siblings up to but not including the element matched by the selector, optionally filtered by another selector.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string | Cheerio | Node | Selector for element to stop at. |
|
filterSelector |
string |
<optional> |
If specified filter for siblings. |
- Source:
The previous nodes.
- Type
- Cheerio
$('.pear').prevUntil('.apple');
//=> [<li class="orange">Orange</li>]
# (static) siblings(selectoropt) → {Cheerio}
Gets the first selected element's siblings, excluding itself.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for siblings. |
- Source:
The siblings.
- Type
- Cheerio
$('.pear').siblings().length;
//=> 2
$('.pear').siblings('.orange').length;
//=> 1
# (static) slice(startopt, endopt) → {Cheerio}
Gets the elements matching the specified range (0-based position).
Name | Type | Attributes | Description |
---|---|---|---|
start |
number |
<optional> |
An position at which the elements begin to be selected. If negative, it indicates an offset from the end of the set. |
end |
number |
<optional> |
An position at which the elements stop being selected. If negative, it indicates an offset from the end of the set. If omitted, the range continues until the end of the set. |
- Source:
The elements matching the specified range.
- Type
- Cheerio
$('li').slice(1).eq(0).text();
//=> 'Orange'
$('li').slice(1, 2).length;
//=> 1