# new Cheerio(selector, contextopt, rootopt, optionsopt)
Instance of cheerio. Methods are specified in the modules. Usage of this constructor is not recommended. Please use $.load instead.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string | Cheerio | Node | Array.<Node> | The new selection. |
|
context |
string | Cheerio | Node | Array.<Node> |
<optional> |
Context of the selection. |
root |
string | Cheerio | Node | Array.<Node> |
<optional> |
Sets the root node. |
options |
object |
<optional> |
Options for the instance. |
Members
# cheerio
Set a signature of the object.
- Source:
Methods
# (static) contains(container, contained) → {boolean}
Checks to see if the contained
DOM element is a descendant of the
container
DOM element.
Name | Type | Description |
---|---|---|
container |
Node | Potential parent node. |
contained |
Node | Potential child node. |
- Source:
Indicates if the nodes contain one another.
- Type
- boolean
# (static) merge(arr1, arr2) → {Array|Cheerio}
$.merge().
Name | Type | Description |
---|---|---|
arr1 |
Array | Cheerio | First array. |
arr2 |
Array | Cheerio | Second array. |
- Source:
arr1
, with elements of arr2
inserted.
- Type
- Array | Cheerio
# (static) parseHTML(data, contextopt, keepScriptsopt) → {Array.<Node>}
Parses a string into an array of DOM nodes. The context
argument has no
meaning for Cheerio, but it is maintained for API compatibility with jQuery.
Name | Type | Attributes | Description |
---|---|---|---|
data |
string | Markup that will be parsed. |
|
context |
any | boolean |
<optional> |
Will be ignored. If it is a boolean it
will be used as the value of |
keepScripts |
boolean |
<optional> |
If false all scripts will be removed. |
- Source:
The parsed DOM.
- Type
- Array.<Node>
# (static) root() → {Cheerio}
Sometimes you need to work with the top-level root element. To query it, you
can use $.root()
.
- Source:
Cheerio instance wrapping the root node.
- Type
- Cheerio
$.root().append('<ul id="vegetables"></ul>').html();
//=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
# 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. |
- Mixes In:
- Source:
The combined set.
- Type
- Cheerio
$('.apple').add('.orange').length;
//=> 2
# 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. |
- Mixes In:
- Source:
The combined set.
- Type
- Cheerio
$('li').eq(0).addBack('.orange').length;
//=> 2
# addClass(value) → {Cheerio}
Adds class(es) to all of the matched elements. Also accepts a function
like jQuery.
Name | Type | Description |
---|---|---|
value |
string | function | Name of new class. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('.pear').addClass('fruit').html();
//=> <li class="pear fruit">Pear</li>
$('.apple').addClass('fruit red').html();
//=> <li class="apple fruit red">Apple</li>
# after(…content) → {Cheerio}
Insert content next to each element in the set of matched elements.
Name | Type | Attributes | Description |
---|---|---|---|
content |
string | Element | Array.<Element> | Cheerio | function |
<repeatable> |
HTML string, DOM element, array of DOM elements or Cheerio to insert after each element in the set of matched elements. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('.apple').after('<li class="plum">Plum</li>');
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="plum">Plum</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>
# append()
Inserts content as the last child of each of the selected elements.
- Mixes In:
- Source:
$('ul').append('<li class="plum">Plum</li>');
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// <li class="plum">Plum</li>
// </ul>
# appendTo(target) → {Cheerio}
Insert every element in the set of matched elements to the end of the target.
Name | Type | Description |
---|---|---|
target |
string | Cheerio | Element to append elements to. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('<li class="plum">Plum</li>').appendTo('#fruits');
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// <li class="plum">Plum</li>
// </ul>
# attr(name, valueopt) → {string|Cheerio}
Method for getting and setting attributes. Gets the attribute value for only
the first element in the matched set. If you set an attribute's value to
null
, you remove that attribute. You may also pass a map
and function
like jQuery.
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | Name of the attribute. |
|
value |
string | function |
<optional> |
If specified sets the value of the attribute. |
- Mixes In:
- Source:
If value
is specified the instance itself,
otherwise the attribute's value.
- Type
- string | Cheerio
$('ul').attr('id');
//=> fruits
$('.apple').attr('id', 'favorite').html();
//=> <li class="apple" id="favorite">Apple</li>
# before(…content) → {Cheerio}
Insert content previous to each element in the set of matched elements.
Name | Type | Attributes | Description |
---|---|---|---|
content |
string | Element | Array.<Element> | Cheerio | function |
<repeatable> |
HTML string, DOM element, array of DOM elements or Cheerio to insert before each element in the set of matched elements. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('.apple').before('<li class="plum">Plum</li>');
$.html();
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>
# children(selectoropt) → {Cheerio}
Gets the children of the first selected element.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for children. |
- Mixes In:
- Source:
The children.
- Type
- Cheerio
$('#fruits').children().length;
//=> 3
$('#fruits').children('.pear').text();
//=> Pear
# clone() → {Cheerio}
Clone the cheerio object.
- Mixes In:
- Source:
The cloned object.
- Type
- Cheerio
const moreFruit = $('#fruits').clone();
# 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. |
- Mixes In:
- 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>]
# contents() → {Cheerio}
Gets the children of each element in the set of matched elements, including text and comment nodes.
- Mixes In:
- Source:
The children.
- Type
- Cheerio
$('#fruits').contents().length;
//=> 3
# css(prop, valopt) → {Cheerio}
Get the value of a style property for the first element in the set of matched elements or set one or more CSS properties for every matched element.
Name | Type | Attributes | Description |
---|---|---|---|
prop |
string | object | The name of the property. |
|
val |
string |
<optional> |
If specified the new value. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
# data(name, valueopt) → {string|Cheerio|undefined}
Method for getting and setting data attributes. Gets or sets the data attribute value for only the first element in the matched set.
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | Name of the attribute. |
|
value |
any |
<optional> |
If specified new value. |
- Mixes In:
- Source:
If value
is specified the instance
itself, otherwise the data attribute's value.
- Type
- string | Cheerio | undefined
$('<div data-apple-color="red"></div>').data();
//=> { appleColor: 'red' }
$('<div data-apple-color="red"></div>').data('apple-color');
//=> 'red'
const apple = $('.apple').data('kind', 'mac');
apple.data('kind');
//=> 'mac'
# 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. |
- Mixes In:
- 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
# empty() → {Cheerio}
Empties an element, removing all its children.
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('ul').empty();
$.html();
//=> <ul id="fruits"></ul>
# end() → {Cheerio}
End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.
- Mixes In:
- Source:
The previous state of the set of matched elements.
- Type
- Cheerio
$('li').eq(0).end().length;
//=> 3
# 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. |
- Mixes In:
- Source:
The element at the i
th position.
- Type
- Cheerio
$('li').eq(0).text();
//=> Apple
$('li').eq(-1).text();
//=> Pear
# 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. |
- Mixes In:
- 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
# 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. |
- Mixes In:
- Source:
The found elements.
- Type
- Cheerio
$('#fruits').find('li').length;
//=> 3
$('#fruits').find($('.apple')).length;
//=> 1
# first() → {Cheerio}
Will select the first element of a cheerio object.
- Mixes In:
- Source:
The first element.
- Type
- Cheerio
$('#fruits').children().first().text();
//=> Apple
# 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. |
- Mixes In:
- 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
# 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. |
- Mixes In:
- Source:
The filtered collection.
- Type
- Cheerio
Selector
$('ul').has('.pear').attr('id');
//=> fruits
Element
$('ul').has($('.pear')[0]).attr('id');
//=> fruits
# hasClass(className) → {boolean}
Check to see if any of the matched elements have the given className
.
Name | Type | Description |
---|---|---|
className |
string | Name of the class. |
- Mixes In:
- Source:
Indicates if an element has the given className
.
- Type
- boolean
$('.pear').hasClass('pear');
//=> true
$('apple').hasClass('fruit');
//=> false
$('li').hasClass('pear');
//=> true
# html(str) → {Cheerio}
Gets an HTML content string from the first selected element. If htmlString
is specified, each selected element's content is replaced by the new content.
Name | Type | Description |
---|---|---|
str |
string | Cheerio | If specified used to replace selection's contents. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('.orange').html();
//=> Orange
$('#fruits').html('<li class="mango">Mango</li>').html();
//=> <li class="mango">Mango</li>
# 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. |
- Mixes In:
- Source:
The index of the element.
- Type
- number
$('.pear').index();
//=> 2
$('.orange').index('li');
//=> 1
$('.apple').index($('#fruit, li'));
//=> 1
# insertAfter(target) → {Cheerio}
Insert every element in the set of matched elements after the target.
Name | Type | Description |
---|---|---|
target |
string | Cheerio | Element to insert elements after. |
- Mixes In:
- Source:
The set of newly inserted elements.
- Type
- Cheerio
$('<li class="plum">Plum</li>').insertAfter('.apple');
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="plum">Plum</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>
# insertBefore(target) → {Cheerio}
Insert every element in the set of matched elements before the target.
Name | Type | Description |
---|---|---|
target |
string | Cheerio | Element to insert elements before. |
- Mixes In:
- Source:
The set of newly inserted elements.
- Type
- Cheerio
$('<li class="plum">Plum</li>').insertBefore('.apple');
$.html();
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>
# is(selector) → {boolean}
Checks the current list of elements and returns true
if any of the
elements match the selector. If using an element or Cheerio selection,
returns true
if any of the elements match. If using a predicate
function, the function is executed in the context of the selected element,
so this
refers to the current element.
Name | Type | Description |
---|---|---|
selector |
string | function | Cheerio | Node | Selector for the selection. |
- Mixes In:
- Source:
Whether or not the selector matches an element of the instance.
- Type
- boolean
# last() → {Cheerio}
Will select the last element of a cheerio object.
- Mixes In:
- Source:
The last element.
- Type
- Cheerio
$('#fruits').children().last().text();
//=> Pear
# 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. |
- Mixes In:
- 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"
# 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. |
- Mixes In:
- Source:
The next nodes.
- Type
- Cheerio
$('.apple').next().hasClass('orange');
//=> true
# 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. |
- Mixes In:
- 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>]
# 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. |
- Mixes In:
- Source:
The next nodes.
- Type
- Cheerio
$('.apple').nextUntil('.pear');
//=> [<li class="orange">Orange</li>]
# 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. |
- Mixes In:
- 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
# 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. |
- Mixes In:
- Source:
The parents.
- Type
- Cheerio
$('.pear').parent().attr('id');
//=> fruits
# 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. |
- Mixes In:
- Source:
The parents.
- Type
- Cheerio
$('.orange').parents().length;
// => 2
$('.orange').parents('#fruits').length;
// => 1
# 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. |
- Mixes In:
- Source:
The parents.
- Type
- Cheerio
$('.orange').parentsUntil('#food').length;
// => 1
# prepend()
Inserts content as the first child of each of the selected elements.
- Mixes In:
- Source:
$('ul').prepend('<li class="plum">Plum</li>');
$.html();
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>
# prependTo(target) → {Cheerio}
Insert every element in the set of matched elements to the beginning of the target.
Name | Type | Description |
---|---|---|
target |
string | Cheerio | Element to prepend elements to. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('<li class="plum">Plum</li>').prependTo('#fruits');
$.html();
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>
# 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. |
- Mixes In:
- Source:
The previous nodes.
- Type
- Cheerio
$('.orange').prev().hasClass('apple');
//=> true
# 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. |
- Mixes In:
- 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>]
# 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. |
- Mixes In:
- Source:
The previous nodes.
- Type
- Cheerio
$('.pear').prevUntil('.apple');
//=> [<li class="orange">Orange</li>]
# prop(name, valueopt) → {string|Cheerio}
Method for getting and setting properties. Gets the property value for only the first element in the matched set.
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | Name of the property. |
|
value |
any |
<optional> |
If specified set the property to this. |
- Mixes In:
- Source:
If value
is specified the instance itself,
otherwise the prop's value.
- Type
- string | Cheerio
$('input[type="checkbox"]').prop('checked');
//=> false
$('input[type="checkbox"]').prop('checked', true).val();
//=> ok
# remove(selectoropt) → {Cheerio}
Removes the set of matched elements from the DOM and all their children.
selector
filters the set of matched elements to be removed.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
Optional selector for elements to remove. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('.pear').remove();
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// </ul>
# removeAttr(name) → {Cheerio}
Method for removing attributes by name
.
Name | Type | Description |
---|---|---|
name |
string | Name of the attribute. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('.pear').removeAttr('class').html();
//=> <li>Pear</li>
$('.apple').attr('id', 'favorite');
$('.apple').removeAttr('id class').html();
//=> <li>Apple</li>
# removeClass(value) → {Cheerio}
Removes one or more space-separated classes from the selected elements. If
no className
is defined, all classes will be removed. Also accepts a
function
like jQuery.
Name | Type | Description |
---|---|---|
value |
string | function | Name of the class. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('.pear').removeClass('pear').html();
//=> <li class="">Pear</li>
$('.apple').addClass('red').removeClass().html();
//=> <li class="">Apple</li>
# replaceWith(content) → {Cheerio}
Replaces matched elements with content
.
Name | Type | Description |
---|---|---|
content |
Cheerio | function | Replacement for matched elements. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
const plum = $('<li class="plum">Plum</li>');
$('.pear').replaceWith(plum);
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="plum">Plum</li>
// </ul>
# serialize() → {string}
Encode a set of form elements as a string for submission.
- Mixes In:
- Source:
The serialized form.
- Type
- string
# serializeArray() → {Array.<object>}
Encode a set of form elements as an array of names and values.
- {Cheerio}
- Mixes In:
- Source:
The serialized form.
- Type
- Array.<object>
$('<form><input name="foo" value="bar" /></form>').serializeArray();
//=> [ { name: 'foo', value: 'bar' } ]
# siblings(selectoropt) → {Cheerio}
Gets the first selected element's siblings, excluding itself.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
If specified filter for siblings. |
- Mixes In:
- Source:
The siblings.
- Type
- Cheerio
$('.pear').siblings().length;
//=> 2
$('.pear').siblings('.orange').length;
//=> 1
# 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. |
- Mixes In:
- Source:
The elements matching the specified range.
- Type
- Cheerio
$('li').slice(1).eq(0).text();
//=> 'Orange'
$('li').slice(1, 2).length;
//=> 1
# text(stropt) → {Cheerio|string}
Get the combined text contents of each element in the set of matched
elements, including their descendants. If textString
is specified, each
selected element's content is replaced by the new text content.
Name | Type | Attributes | Description |
---|---|---|---|
str |
string | function |
<optional> |
If specified replacement for the selected element's contents. |
- Mixes In:
- Source:
The instance itself when setting text, otherwise the rendered document.
- Type
- Cheerio | string
$('.orange').text();
//=> Orange
$('ul').text();
//=> Apple
// Orange
// Pear
# toArray() → {Array.<Node>}
Retrieve all the DOM elements contained in the jQuery set as an array.
- Source:
The contained items.
- Type
- Array.<Node>
$('li').toArray(); //=> [ {...}, {...}, {...} ]
# toggleClass(value, stateValopt) → {Cheerio}
Add or remove class(es) from the matched elements, depending on either the
class's presence or the value of the switch argument. Also accepts a
function
like jQuery.
Name | Type | Attributes | Description |
---|---|---|---|
value |
string | function | Name of the class. Can also be a function. |
|
stateVal |
boolean |
<optional> |
If specified the state of the class. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
$('.apple.green').toggleClass('fruit green red').html();
//=> <li class="apple fruit red">Apple</li>
$('.apple.green').toggleClass('fruit green red', true).html();
//=> <li class="apple green fruit red">Apple</li>
# toString() → {string}
Turns the collection to a string. Alias for .html()
.
- Mixes In:
- Source:
The rendered document.
- Type
- string
# unwrap(selectoropt) → {Cheerio}
The .unwrap() function, removes the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
Name | Type | Attributes | Description |
---|---|---|---|
selector |
string |
<optional> |
A selector to check the parent element against. If an element's parent does not match the selector, the element won't be unwrapped. |
- Mixes In:
- Source:
The instance itself, for chaining.
- Type
- Cheerio
without selector
const $ = cheerio.load(
'<div id=test>\n <div><p>Hello</p></div>\n <div><p>World</p></div>\n</div>'
);
$('#test p').unwrap();
//=> <div id=test>
// <p>Hello</p>
// <p>World</p>
// </div>
with selector
const $ = cheerio.load(
'<div id=test>\n <p>Hello</p>\n <b><p>World</p></b>\n</div>'
);
$('#test p').unwrap('b');
//=> <div id=test>
// <p>Hello</p>
// <p>World</p>
// </div>
# val(valueopt) → {string|Cheerio|undefined}
Method for getting and setting the value of input, select, and textarea.
Note: Support for map
, and function
has not been added yet.
Name | Type | Attributes | Description |
---|---|---|---|
value |
string | Array.<string> |
<optional> |
If specified new value. |
- Mixes In:
- Source:
If a new value
is specified the
instance itself, otherwise the value.
- Type
- string | Cheerio | undefined
$('input[type="text"]').val();
//=> input_text
$('input[type="text"]').val('test').html();
//=> <input type="text" value="test"/>
# wrap(wrapper)
The .wrap() function can take any string or object that could be passed to the $() factory function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. A copy of this structure will be wrapped around each of the elements in the set of matched elements. This method returns the original set of elements for chaining purposes.
Name | Type | Description |
---|---|---|
wrapper |
Cheerio | The DOM structure to wrap around each element in the selection. |
- Mixes In:
- Source:
const redFruit = $('<div class="red-fruit"></div>');
$('.apple').wrap(redFruit);
//=> <ul id="fruits">
// <div class="red-fruit">
// <li class="apple">Apple</li>
// </div>
// <li class="orange">Orange</li>
// <li class="plum">Plum</li>
// </ul>
const healthy = $('<div class="healthy"></div>');
$('li').wrap(healthy);
//=> <ul id="fruits">
// <div class="healthy">
// <li class="apple">Apple</li>
// </div>
// <div class="healthy">
// <li class="orange">Orange</li>
// </div>
// <div class="healthy">
// <li class="plum">Plum</li>
// </div>
// </ul>
# wrapAll(wrapper) → {Cheerio}
The .wrapAll() function can take any string or object that could be passed to the $() function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. The structure will be wrapped around all of the elements in the set of matched elements, as a single group.
Name | Type | Description |
---|---|---|
wrapper |
Cheerio | string | Element | Array.<Element> | function | The DOM structure to wrap around all matched elements in the selection. |
- Mixes In:
- Source:
The instance itself.
- Type
- Cheerio
With markup passed to `wrapAll`
const $ = cheerio.load(
'<div class="container"><div class="inner">First</div><div class="inner">Second</div></div>'
);
$('.inner').wrapAll("<div class='new'></div>");
//=> <div class="container">
// <div class='new'>
// <div class="inner">First</div>
// <div class="inner">Second</div>
// </div>
// </div>
With an existing cheerio instance
const $ = cheerio.load(
'<span>Span 1</span><strong>Strong</strong><span>Span 2</span>'
);
const wrap = $('<div><p><em><b></b></em></p></div>');
$('span').wrapAll(wrap);
//=> <div>
// <p>
// <em>
// <b>
// <span>Span 1</span>
// <span>Span 2</span>
// </b>
// </em>
// </p>
// </div>
// <strong>Strong</strong>
# wrapInner(wrapper)
The .wrapInner() function can take any string or object that could be passed to the $() factory function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. The structure will be wrapped around the content of each of the elements in the set of matched elements.
Name | Type | Description |
---|---|---|
wrapper |
Cheerio | The DOM structure to wrap around the content of each element in the selection. |
- Mixes In:
- Source:
const redFruit = $('<div class="red-fruit"></div>');
$('.apple').wrapInner(redFruit);
//=> <ul id="fruits">
// <li class="apple">
// <div class="red-fruit">Apple</div>
// </li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>
const healthy = $('<div class="healthy"></div>');
$('li').wrapInner(healthy);
//=> <ul id="fruits">
// <li class="apple">
// <div class="healthy">Apple</div>
// </li>
// <li class="orange">
// <div class="healthy">Orange</div>
// </li>
// <li class="pear">
// <div class="healthy">Pear</div>
// </li>
// </ul>