AbstractMethod for getting attributes. Gets the attribute value for only the first element in the matched set.
The attribute's value.
Method for setting attributes. Sets the attribute value for all elements 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.
The instance itself.
Method for setting multiple attributes at once. Sets the attribute value for
all elements in the matched set. If you set an attribute's value to null,
you remove that attribute.
The instance itself.
Method for getting data attributes, for only the first element in the matched set.
The data attribute's value, or undefined if the attribute does not
exist.
Check to see if any of the matched elements have the given className.
Indicates if an element has the given className.
Method for getting and setting properties. Gets the property value for only the first element in the matched set.
If value is specified the instance itself, otherwise the prop's
value.
Method for getting and setting properties. Gets the property value for only the first element in the matched set.
If value is specified the instance itself, otherwise the prop's
value.
Resolve href or src of supported elements. Requires the baseURI option
to be set, and a global URL object to be part of the environment.
The resolved URL, or undefined if the element is not supported.
Set a property of an element.
Name of the property.
Value to set the property to.
The instance itself.
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.
The instance itself.
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.
The instance itself.
Method for getting the value of input, select, and textarea. Note: Support
for map, and function has not been added yet.
The value.
Get the value of a style property for the first element in the set of matched elements.
The property value for the given name.
Encode a set of form elements as a string for submission.
The serialized form.
Encode a set of form elements as an array of names and values.
The serialized form.
Inserts content as the last child of each of the selected elements.
Inserts content as the first child of each of the selected elements.
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.
The DOM structure to wrap around each element in the selection.
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>
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.
The DOM structure to wrap around the content of each element in the selection.
The instance itself, for chaining.
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>
Insert content next to each element in the set of matched elements.
HTML string, DOM element, array of DOM elements or Cheerio to insert after each element in the set of matched elements.
The instance itself.
Insert content previous to each element in the set of matched elements.
HTML string, DOM element, array of DOM elements or Cheerio to insert before each element in the set of matched elements.
The instance itself.
Gets an HTML content string from the first selected element.
The HTML content string.
Insert every element in the set of matched elements before the target.
Element to insert elements before.
The set of newly inserted elements.
Get the combined text contents of each element in the set of matched elements, including their descendants.
The text contents of the collection.
The .unwrap() function, removes the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
The instance itself, for chaining.
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>
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.
The DOM structure to wrap around all matched elements in the selection.
The instance itself.
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>
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>
Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
The zero-based location in the array from which to start removing elements.
OptionaldeleteCount: numberThe number of elements to remove. Omitting this argument will remove all elements from the start paramater location to end of the array. If value of this argument is either a negative number, zero, undefined, or a type that cannot be converted to an integer, the function will evaluate the argument as zero and not remove any elements.
An array containing the elements that were deleted.
Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
The zero-based location in the array from which to start removing elements.
The number of elements to remove. If value of this argument is either a negative number, zero, undefined, or a type that cannot be converted to an integer, the function will evaluate the argument as zero and not remove any elements.
Elements to insert into the array in place of the deleted elements.
An array containing the elements that were deleted.
Extract multiple values from a document, and store them in an object.
An object containing the extracted values.
Gets the element children of each element in the set of matched elements.
Gets the next sibling of each selected element, optionally filtered by a selector.
Gets all the following siblings of the each selected element, optionally filtered by a selector.
Gets all the following siblings up to but not including the element matched by the selector, optionally filtered by another selector.
Optionalselector: AcceptedFilters<Element> | nullSelector for element to stop at.
OptionalfilterSelector: AcceptedFilters<Element>If specified filter for siblings.
The next nodes.
Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
Get a set of parents filtered by selector of each element in the current
set of match elements.
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.
Optionalselector: AcceptedFilters<Element> | nullSelector for element to stop at.
OptionalfilterSelector: AcceptedFilters<Element>Optional filter for parents.
The parents.
Gets the previous sibling of each selected element optionally filtered by a selector.
Gets all the preceding siblings of each selected element, optionally filtered by a selector.
Gets all the preceding siblings up to but not including the element matched by the selector, optionally filtered by another selector.
Optionalselector: AcceptedFilters<Element> | nullSelector for element to stop at.
OptionalfilterSelector: AcceptedFilters<Element>If specified filter for siblings.
The previous nodes.
Get the siblings of each element (excluding the element) in the set of matched elements, optionally filtered by a selector.
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.
Optionalselector: AcceptedFilters<Element>Selector for the element to find.
The closest nodes.
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.
The instance itself, useful for chaining.
Iterates over a cheerio object, reducing the set of selector elements to those that match the selector or pass the function's test.
This is the definition for using type guards; have a look below for other
ways to invoke this method. The function is executed in the context of the
selected element, so this refers to the current element.
The filtered collection.
Iterates over a cheerio object, reducing the set of selector elements to those that match the selector or pass the function's test.
this refers to the current element.Value to look for, following the rules above. See AcceptedFilters.
The filtered collection.
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.
Optionalselector: AcceptedFilters<T>Selector for the selection.
Whether or not the selector matches an element of the instance.
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.
The mapped elements, wrapped in a Cheerio collection.
Remove elements from the set of matched elements. Given a Cheerio object that
represents a set of DOM elements, the .not() method constructs a new
Cheerio 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.
Value to look for, following the rules above.
The filtered collection.
Gets the elements matching the specified range (0-based position).
Optionalstart: numberA position at which the elements begin to be selected. If negative, it indicates an offset from the end of the set.
Optionalend: numberA 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.
The elements matching the specified range.
The cheerio class is the central class of the library. It wraps a set of elements and provides an API for traversing, modifying, and interacting with the set.
Loading a document will return the Cheerio class bound to the root element of the document. The class will be instantiated when querying the document (when calling
$('selector')).Example: This is the HTML markup we will be using in all of the API examples: