Skip to main content

Class: abstract Cheerio<T>

Defined in: src/cheerio.ts:39

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​

<ul id="fruits">
<li class="apple">Apple</li>
<li class="orange">Orange</li>
<li class="pear">Pear</li>
</ul>

Extends​

Type Parameters​

T​

T

Implements​

  • ArrayLike<T>

Indexable​

[index: number]: T

Attributes​

addClass()​

addClass<T, R>(this, value?): R

Defined in: src/api/attributes.ts:947

Adds class(es) to all of the matched elements. Also accepts a function.

Type Parameters​

T​

T extends AnyNode

R​

R extends ArrayLike<T>

Parameters​

this​

R

value?​

Name of new class.

string | (this, i, className) => undefined | string

Returns​

R

The instance itself.

Example​

$('.pear').addClass('fruit').prop('outerHTML');
//=> <li class="pear fruit">Pear</li>

$('.apple').addClass('fruit red').prop('outerHTML');
//=> <li class="apple fruit red">Apple</li>

See​

https://api.jquery.com/addClass/


attr()​

Call Signature​

attr<T>(this, name): undefined | string

Defined in: src/api/attributes.ts:119

Method for getting attributes. Gets the attribute value for only the first element in the matched set.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

string

Name of the attribute.

Returns​

undefined | string

The attribute's value.

Example​
$('ul').attr('id');
//=> fruits
See​

https://api.jquery.com/attr/

Call Signature​

attr<T>(this): undefined | Record<string, string>

Defined in: src/api/attributes.ts:138

Method for getting all attributes and their values of the first element in the matched set.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

Returns​

undefined | Record<string, string>

The attribute's values.

Example​
$('ul').attr();
//=> { id: 'fruits' }
See​

https://api.jquery.com/attr/

Call Signature​

attr<T>(this, name, value?): Cheerio<T>

Defined in: src/api/attributes.ts:159

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.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

string

Name of the attribute.

value?​

The new value of the attribute.

null | string | (this, i, attrib) => null | string

Returns​

Cheerio<T>

The instance itself.

Example​
$('.apple').attr('id', 'favorite').prop('outerHTML');
//=> <li class="apple" id="favorite">Apple</li>
See​

https://api.jquery.com/attr/

Call Signature​

attr<T>(this, values): Cheerio<T>

Defined in: src/api/attributes.ts:184

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.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

values​

Record<string, string | null>

Map of attribute names and values.

Returns​

Cheerio<T>

The instance itself.

Example​
$('.apple').attr({ id: 'favorite' }).prop('outerHTML');
//=> <li class="apple" id="favorite">Apple</li>
See​

https://api.jquery.com/attr/


data()​

Call Signature​

data<T>(this, name): unknown

Defined in: src/api/attributes.ts:643

Method for getting data attributes, for only the first element in the matched set.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

string

Name of the data attribute.

Returns​

unknown

The data attribute's value, or undefined if the attribute does not exist.

Example​
$('<div data-apple-color="red"></div>').data('apple-color');
//=> 'red'
See​

https://api.jquery.com/data/

Call Signature​

data<T>(this): Record<string, unknown>

Defined in: src/api/attributes.ts:662

Method for getting all of an element's data attributes, for only the first element in the matched set.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

Returns​

Record<string, unknown>

A map with all of the data attributes.

Example​
$('<div data-apple-color="red"></div>').data();
//=> { appleColor: 'red' }
See​

https://api.jquery.com/data/

Call Signature​

data<T>(this, name, value): Cheerio<T>

Defined in: src/api/attributes.ts:684

Method for setting data attributes, for only the first element in the matched set.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

string

Name of the data attribute.

value​

unknown

The new value.

Returns​

Cheerio<T>

The instance itself.

Example​
const apple = $('.apple').data('kind', 'mac');

apple.data('kind');
//=> 'mac'
See​

https://api.jquery.com/data/

Call Signature​

data<T>(this, values): Cheerio<T>

Defined in: src/api/attributes.ts:707

Method for setting multiple data attributes at once, for only the first element in the matched set.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

values​

Record<string, unknown>

Map of names to values.

Returns​

Cheerio<T>

The instance itself.

Example​
const apple = $('.apple').data({ kind: 'mac' });

apple.data('kind');
//=> 'mac'
See​

https://api.jquery.com/data/


hasClass()​

hasClass<T>(this, className): boolean

Defined in: src/api/attributes.ts:904

Check to see if any of the matched elements have the given className.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

className​

string

Name of the class.

Returns​

boolean

Indicates if an element has the given className.

Example​

$('.pear').hasClass('pear');
//=> true

$('apple').hasClass('fruit');
//=> false

$('li').hasClass('pear');
//=> true

See​

https://api.jquery.com/hasClass/


prop()​

Call Signature​

prop<T>(this, name): undefined | string

Defined in: src/api/attributes.ts:302

Method for getting and setting properties. Gets the property value for only the first element in the matched set.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

Name of the property.

"tagName" | "nodeName"

Returns​

undefined | string

If value is specified the instance itself, otherwise the prop's value.

Example​
$('input[type="checkbox"]').prop('checked');
//=> false

$('input[type="checkbox"]').prop('checked', true).val();
//=> ok
See​

https://api.jquery.com/prop/

Call Signature​

prop<T>(this, name): null | string

Defined in: src/api/attributes.ts:306

Method for getting and setting properties. Gets the property value for only the first element in the matched set.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

Name of the property.

"innerText" | "innerHTML" | "outerHTML" | "textContent"

Returns​

null | string

If value is specified the instance itself, otherwise the prop's value.

Example​
$('input[type="checkbox"]').prop('checked');
//=> false

$('input[type="checkbox"]').prop('checked', true).val();
//=> ok
See​

https://api.jquery.com/prop/

Call Signature​

prop<T>(this, name): undefined | StyleProp

Defined in: src/api/attributes.ts:317

Get a parsed CSS style object.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

"style"

Name of the property.

Returns​

undefined | StyleProp

The style object, or undefined if the element has no style attribute.

Call Signature​

prop<T>(this, name): undefined | string

Defined in: src/api/attributes.ts:335

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.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

Name of the property.

"href" | "src"

Returns​

undefined | string

The resolved URL, or undefined if the element is not supported.

Example​
$('<img src="image.png">').prop('src');
//=> 'https://example.com/image.png'

Call Signature​

prop<T, K>(this, name): Element[K]

Defined in: src/api/attributes.ts:345

Get a property of an element.

Type Parameters​
T​

T extends AnyNode

K​

K extends keyof Element

Parameters​
this​

Cheerio<T>

name​

K

Name of the property.

Returns​

Element[K]

The property's value.

Call Signature​

prop<T, K>(this, name, value): Cheerio<T>

Defined in: src/api/attributes.ts:356

Set a property of an element.

Type Parameters​
T​

T extends AnyNode

K​

K extends keyof Element

Parameters​
this​

Cheerio<T>

name​

K

Name of the property.

value​

Value to set the property to.

Element[K] | (this, i, prop) => undefined | null | string | number | Record<string, string> | TagSourceCodeLocation | Document | Element | CDATA | Text | Comment | ProcessingInstruction | ChildNode[] | {[name: string]: string; } | Attribute[] | <T>(this, recursive?) => T

Returns​

Cheerio<T>

The instance itself.

Call Signature​

prop<T>(this, map): Cheerio<T>

Defined in: src/api/attributes.ts:378

Set multiple properties of an element.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

map​

Record<string, string | Element[keyof Element] | boolean>

Object of properties to set.

Returns​

Cheerio<T>

The instance itself.

Example​
$('input[type="checkbox"]').prop({
checked: true,
disabled: false,
});

Call Signature​

prop<T>(this, name, value): Cheerio<T>

Defined in: src/api/attributes.ts:389

Set a property of an element.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

string

Name of the property.

value​

Value to set the property to.

null | string | boolean | (this, i, prop) => string | boolean

Returns​

Cheerio<T>

The instance itself.

Call Signature​

prop<T>(this, name): string

Defined in: src/api/attributes.ts:404

Get a property of an element.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

string

The property's name.

Returns​

string

The property's value.


removeAttr()​

removeAttr<T>(this, name): Cheerio<T>

Defined in: src/api/attributes.ts:868

Method for removing attributes by name.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

name​

string

Name of the attribute.

Returns​

Cheerio<T>

The instance itself.

Example​

$('.pear').removeAttr('class').prop('outerHTML');
//=> <li>Pear</li>

$('.apple').attr('id', 'favorite');
$('.apple').removeAttr('id class').prop('outerHTML');
//=> <li>Apple</li>

See​

https://api.jquery.com/removeAttr/


removeClass()​

removeClass<T, R>(this, name?): R

Defined in: src/api/attributes.ts:1015

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.

Type Parameters​

T​

T extends AnyNode

R​

R extends ArrayLike<T>

Parameters​

this​

R

name?​

Name of the class. If not specified, removes all elements.

string | (this, i, className) => undefined | string

Returns​

R

The instance itself.

Example​

$('.pear').removeClass('pear').prop('outerHTML');
//=> <li class="">Pear</li>

$('.apple').addClass('red').removeClass().prop('outerHTML');
//=> <li class="">Apple</li>

See​

https://api.jquery.com/removeClass/


toggleClass()​

toggleClass<T, R>(this, value?, stateVal?): R

Defined in: src/api/attributes.ts:1086

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.

Type Parameters​

T​

T extends AnyNode

R​

R extends ArrayLike<T>

Parameters​

this​

R

value?​

Name of the class. Can also be a function.

string | (this, i, className, stateVal?) => string

stateVal?​

boolean

If specified the state of the class.

Returns​

R

The instance itself.

Example​

$('.apple.green').toggleClass('fruit green red').prop('outerHTML');
//=> <li class="apple fruit red">Apple</li>

$('.apple.green').toggleClass('fruit green red', true).prop('outerHTML');
//=> <li class="apple green fruit red">Apple</li>

See​

https://api.jquery.com/toggleClass/


val()​

Call Signature​

val<T>(this): undefined | string | string[]

Defined in: src/api/attributes.ts:757

Method for getting the value of input, select, and textarea. Note: Support for map, and function has not been added yet.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

Returns​

undefined | string | string[]

The value.

Example​
$('input[type="text"]').val();
//=> input_text
See​

https://api.jquery.com/val/

Call Signature​

val<T>(this, value): Cheerio<T>

Defined in: src/api/attributes.ts:776

Method for setting the value of input, select, and textarea. Note: Support for map, and function has not been added yet.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

value​

The new value.

string | string[]

Returns​

Cheerio<T>

The instance itself.

Example​
$('input[type="text"]').val('test').prop('outerHTML');
//=> <input type="text" value="test"/>
See​

https://api.jquery.com/val/

CSS​

css()​

Set multiple CSS properties for every matched element.

Param​

The names of the properties.

Param​

The new values.

See​

https://api.jquery.com/css/

Call Signature​

css<T>(this, names?): undefined | Record<string, string>

Defined in: src/api/css.ts:14

Get the value of a style property for the first element in the set of matched elements.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

names?​

string[]

Optionally the names of the properties of interest.

Returns​

undefined | Record<string, string>

The instance itself.

Param​

The names of the properties.

Param​

The new values.

See​

https://api.jquery.com/css/

See​

https://api.jquery.com/css/

Call Signature​

css<T>(this, name): undefined | string

Defined in: src/api/css.ts:27

Get the value of a style property for the first element in the set of matched elements.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

name​

string

The name of the property.

Returns​

undefined | string

The instance itself.

Param​

The names of the properties.

Param​

The new values.

See​

https://api.jquery.com/css/

See​

https://api.jquery.com/css/

Call Signature​

css<T>(this, prop, val): Cheerio<T>

Defined in: src/api/css.ts:40

Set one CSS property for every matched element.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

prop​

string

The name of the property.

val​

The new value.

string | (this, i, style) => undefined | string

Returns​

Cheerio<T>

The instance itself.

Param​

The names of the properties.

Param​

The new values.

See​

https://api.jquery.com/css/

See​

https://api.jquery.com/css/

Call Signature​

css<T>(this, map): Cheerio<T>

Defined in: src/api/css.ts:55

Set multiple CSS properties for every matched element.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

map​

Record<string, string>

A map of property names and values.

Returns​

Cheerio<T>

The instance itself.

Param​

The names of the properties.

Param​

The new values.

See​

https://api.jquery.com/css/

See​

https://api.jquery.com/css/

Forms​

serialize()​

serialize<T>(this): string

Defined in: src/api/forms.ts:26

Encode a set of form elements as a string for submission.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

Returns​

string

The serialized form.

Example​

$('<form><input name="foo" value="bar" /></form>').serialize();
//=> 'foo=bar'

See​

https://api.jquery.com/serialize/


serializeArray()​

serializeArray<T>(this): object[]

Defined in: src/api/forms.ts:54

Encode a set of form elements as an array of names and values.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

Returns​

object[]

The serialized form.

Example​

$('<form><input name="foo" value="bar" /></form>').serializeArray();
//=> [ { name: 'foo', value: 'bar' } ]

See​

https://api.jquery.com/serializeArray/

Manipulation​

append()​

append: <T>(this, ...elems) => Cheerio<T>

Defined in: src/api/manipulation.ts:271

Inserts content as the last child of each of the selected elements.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

elems​

[(this, i, html) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]

Returns​

Cheerio<T>

Example​

$('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>

See​

https://api.jquery.com/append/


prepend()​

prepend: <T>(this, ...elems) => Cheerio<T>

Defined in: src/api/manipulation.ts:299

Inserts content as the first child of each of the selected elements.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

elems​

[(this, i, html) => BasicAcceptedElems<AnyNode>] | BasicAcceptedElems<AnyNode>[]

Returns​

Cheerio<T>

Example​

$('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>

See​

https://api.jquery.com/prepend/


wrap()​

wrap: <T>(this, wrapper) => Cheerio<T>

Defined in: src/api/manipulation.ts:404

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.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

wrapper​

AcceptedElems<AnyNode>

The DOM structure to wrap around each element in the selection.

Returns​

Cheerio<T>

Example​

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>

See​

https://api.jquery.com/wrap/


wrapInner()​

wrapInner: <T>(this, wrapper) => Cheerio<T>

Defined in: src/api/manipulation.ts:467

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.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

wrapper​

AcceptedElems<AnyNode>

The DOM structure to wrap around the content of each element in the selection.

Returns​

Cheerio<T>

The instance itself, for chaining.

Example​

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>

See​

https://api.jquery.com/wrapInner/


after()​

after<T>(this, ...elems): Cheerio<T>

Defined in: src/api/manipulation.ts:640

Insert content next to each element in the set of matched elements.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

elems​

HTML string, DOM element, array of DOM elements or Cheerio to insert after each element in the set of matched elements.

BasicAcceptedElems<AnyNode>[] | [(this, i, html) => BasicAcceptedElems<AnyNode>]

Returns​

Cheerio<T>

The instance itself.

Example​

$('.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>

See​

https://api.jquery.com/after/


appendTo()​

appendTo<T>(this, target): Cheerio<T>

Defined in: src/api/manipulation.ts:208

Insert every element in the set of matched elements to the end of the target.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

target​

BasicAcceptedElems<AnyNode>

Element to append elements to.

Returns​

Cheerio<T>

The instance itself.

Example​

$('<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>

See​

https://api.jquery.com/appendTo/


before()​

before<T>(this, ...elems): Cheerio<T>

Defined in: src/api/manipulation.ts:749

Insert content previous to each element in the set of matched elements.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

elems​

HTML string, DOM element, array of DOM elements or Cheerio to insert before each element in the set of matched elements.

BasicAcceptedElems<AnyNode>[] | [(this, i, html) => BasicAcceptedElems<AnyNode>]

Returns​

Cheerio<T>

The instance itself.

Example​

$('.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>

See​

https://api.jquery.com/before/


clone()​

clone<T>(this): Cheerio<T>

Defined in: src/api/manipulation.ts:1102

Clone the cheerio object.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

Returns​

Cheerio<T>

The cloned object.

Example​

const moreFruit = $('#fruits').clone();

See​

https://api.jquery.com/clone/


empty()​

empty<T>(this): Cheerio<T>

Defined in: src/api/manipulation.ts:938

Removes all children from each item in the selection. Text nodes and comment nodes are left as is.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

Returns​

Cheerio<T>

The instance itself.

Example​

$('ul').empty();
$.html();
//=> <ul id="fruits"></ul>

See​

https://api.jquery.com/empty/


html()​

Call Signature​

html<T>(this): null | string

Defined in: src/api/manipulation.ts:966

Gets an HTML content string from the first selected element.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

Returns​

null | string

The HTML content string.

Example​
$('.orange').html();
//=> Orange

$('#fruits').html('<li class="mango">Mango</li>').html();
//=> <li class="mango">Mango</li>
See​

https://api.jquery.com/html/

Call Signature​

html<T>(this, str): Cheerio<T>

Defined in: src/api/manipulation.ts:982

Replaces each selected element's content with the specified content.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

str​

The content to replace selection's contents with.

string | Cheerio<T>

Returns​

Cheerio<T>

The instance itself.

Example​
$('.orange').html('<li class="mango">Mango</li>').html();
//=> <li class="mango">Mango</li>
See​

https://api.jquery.com/html/


insertAfter()​

insertAfter<T>(this, target): Cheerio<T>

Defined in: src/api/manipulation.ts:693

Insert every element in the set of matched elements after the target.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

target​

BasicAcceptedElems<AnyNode>

Element to insert elements after.

Returns​

Cheerio<T>

The set of newly inserted elements.

Example​

$('<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>

See​

https://api.jquery.com/insertAfter/


insertBefore()​

insertBefore<T>(this, target): Cheerio<T>

Defined in: src/api/manipulation.ts:802

Insert every element in the set of matched elements before the target.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

target​

BasicAcceptedElems<AnyNode>

Element to insert elements before.

Returns​

Cheerio<T>

The set of newly inserted elements.

Example​

$('<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>

See​

https://api.jquery.com/insertBefore/


prependTo()​

prependTo<T>(this, target): Cheerio<T>

Defined in: src/api/manipulation.ts:241

Insert every element in the set of matched elements to the beginning of the target.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

target​

BasicAcceptedElems<AnyNode>

Element to prepend elements to.

Returns​

Cheerio<T>

The instance itself.

Example​

$('<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>

See​

https://api.jquery.com/prependTo/


remove()​

remove<T>(this, selector?): Cheerio<T>

Defined in: src/api/manipulation.ts:854

Removes the set of matched elements from the DOM and all their children. selector filters the set of matched elements to be removed.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

string

Optional selector for elements to remove.

Returns​

Cheerio<T>

The instance itself.

Example​

$('.pear').remove();
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// </ul>

See​

https://api.jquery.com/remove/


replaceWith()​

replaceWith<T>(this, content): Cheerio<T>

Defined in: src/api/manipulation.ts:890

Replaces matched elements with content.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

content​

AcceptedElems<AnyNode>

Replacement for matched elements.

Returns​

Cheerio<T>

The instance itself.

Example​

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>

See​

https://api.jquery.com/replaceWith/


text()​

Call Signature​

text<T>(this): string

Defined in: src/api/manipulation.ts:1040

Get the combined text contents of each element in the set of matched elements, including their descendants.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

Returns​

string

The text contents of the collection.

Example​
$('.orange').text();
//=> Orange

$('ul').text();
//=> Apple
// Orange
// Pear
See​

https://api.jquery.com/text/

Call Signature​

text<T>(this, str): Cheerio<T>

Defined in: src/api/manipulation.ts:1057

Set the content of each element in the set of matched elements to the specified text.

Type Parameters​
T​

T extends AnyNode

Parameters​
this​

Cheerio<T>

str​

The text to set as the content of each matched element.

string | (this, i, text) => string

Returns​

Cheerio<T>

The instance itself.

Example​
$('.orange').text('Orange');
//=> <div class="orange">Orange</div>
See​

https://api.jquery.com/text/


toString()​

toString<T>(this): string

Defined in: src/api/manipulation.ts:1016

Turns the collection to a string. Alias for .html().

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

Returns​

string

The rendered document.


unwrap()​

unwrap<T>(this, selector?): Cheerio<T>

Defined in: src/api/manipulation.ts:515

The .unwrap() function, removes the parents of the set of matched elements from the DOM, leaving the matched elements in their place.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

string

A selector to check the parent element against. If an element's parent does not match the selector, the element won't be unwrapped.

Returns​

Cheerio<T>

The instance itself, for chaining.

Examples​

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>
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>

See​

https://api.jquery.com/unwrap/


wrapAll()​

wrapAll<T>(this, wrapper): Cheerio<T>

Defined in: src/api/manipulation.ts:578

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.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

wrapper​

AcceptedElems<T>

The DOM structure to wrap around all matched elements in the selection.

Returns​

Cheerio<T>

The instance itself.

Examples​

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>

See​

https://api.jquery.com/wrapAll/

Other​

cheerio​

cheerio: "[cheerio object]"

Defined in: src/cheerio.ts:118


length​

length: number = 0

Defined in: src/cheerio.ts:40

Implementation of​

ArrayLike.length


options​

options: InternalOptions

Defined in: src/cheerio.ts:43


prevObject​

prevObject: undefined | Cheerio<any>

Defined in: src/cheerio.ts:77


splice()​

splice: {(start, deleteCount?): any[]; (start, deleteCount, ...items): any[]; }

Defined in: src/cheerio.ts:120

Call Signature​

(start, deleteCount?): any[]

Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.

Parameters​
start​

number

The zero-based location in the array from which to start removing elements.

deleteCount?​

number

The number of elements to remove.

Returns​

any[]

An array containing the elements that were deleted.

Call Signature​

(start, deleteCount, ...items): any[]

Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.

Parameters​
start​

number

The zero-based location in the array from which to start removing elements.

deleteCount​

number

The number of elements to remove.

items​

...any[]

Elements to insert into the array in place of the deleted elements.

Returns​

any[]

An array containing the elements that were deleted.


[iterator]()​

[iterator](): Iterator<T, any, any>

Defined in: website/node_modules/typescript/lib/lib.es2015.iterable.d.ts:49

Returns​

Iterator<T, any, any>


extract()​

extract<M, T>(this, map): ExtractedMap<M>

Defined in: src/api/extract.ts:62

Extract multiple values from a document, and store them in an object.

Type Parameters​

M​

M extends ExtractMap

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

map​

M

An object containing key-value pairs. The keys are the names of the properties to be created on the object, and the values are the selectors to be used to extract the values.

Returns​

ExtractedMap<M>

An object containing the extracted values.


filterArray()​

filterArray<T>(nodes, match, xmlMode?, root?): Element[] | T[]

Defined in: src/api/traversing.ts:789

Type Parameters​

T​

T

Parameters​

nodes​

T[]

match​

AcceptedFilters<T>

xmlMode?​

boolean

root?​

Document

Returns​

Element[] | T[]


foo()​

foo(this): void

Defined in: src/cheerio.spec.ts:13

Parameters​

this​

void

Returns​

void


myPlugin()​

myPlugin(...args): object

Defined in: src/cheerio.spec.ts:9

Parameters​

args​

...unknown[]

Returns​

object

args​

args: unknown[]

context​

context: Cheerio<T>


toArray()​

toArray<T>(this): T[]

Defined in: src/api/traversing.ts:1029

Retrieve all the DOM elements contained in the jQuery set as an array.

Type Parameters​

T​

T

Parameters​

this​

Cheerio<T>

Returns​

T[]

The contained items.

Example​

$('li').toArray();
//=> [ {...}, {...}, {...} ]

Traversing​

children()​

children: <T>(this, selector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:583

Gets the element children of each element in the set of matched elements.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<Element>

If specified filter for children.

Returns​

Cheerio<Element>

The children.

Example​

$('#fruits').children().length;
//=> 3

$('#fruits').children('.pear').text();
//=> Pear

See​

https://api.jquery.com/children/


next()​

next: <T>(this, selector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:398

Gets the next sibling of each selected element, optionally filtered by a selector.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<Element>

If specified filter for sibling.

Returns​

Cheerio<Element>

The next nodes.

Example​

$('.apple').next().hasClass('orange');
//=> true

See​

https://api.jquery.com/next/


nextAll()​

nextAll: <T>(this, selector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:421

Gets all the following siblings of the each selected element, optionally filtered by a selector.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<Element>

If specified filter for siblings.

Returns​

Cheerio<Element>

The next nodes.

Example​

$('.apple').nextAll();
//=> [<li class="orange">Orange</li>, <li class="pear">Pear</li>]
$('.apple').nextAll('.orange');
//=> [<li class="orange">Orange</li>]

See​

https://api.jquery.com/nextAll/


nextUntil()​

nextUntil: <T>(this, selector?, filterSelector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:450

Gets all the following siblings up to but not including the element matched by the selector, optionally filtered by another selector.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

Selector for element to stop at.

AcceptedFilters<Element> | null

filterSelector?​

AcceptedFilters<Element>

If specified filter for siblings.

Returns​

Cheerio<Element>

The next nodes.

Example​

$('.apple').nextUntil('.pear');
//=> [<li class="orange">Orange</li>]

See​

https://api.jquery.com/nextUntil/


parent()​

parent: <T>(this, selector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:245

Get the parent of each element in the current set of matched elements, optionally filtered by a selector.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<Element>

If specified filter for parent.

Returns​

Cheerio<Element>

The parents.

Example​

$('.pear').parent().attr('id');
//=> fruits

See​

https://api.jquery.com/parent/


parents()​

parents: <T>(this, selector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:271

Get a set of parents filtered by selector of each element in the current set of match elements.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<Element>

If specified filter for parents.

Returns​

Cheerio<Element>

The parents.

Example​

$('.orange').parents().length;
//=> 2
$('.orange').parents('#fruits').length;
//=> 1

See​

https://api.jquery.com/parents/


parentsUntil()​

parentsUntil: <T>(this, selector?, filterSelector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:306

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.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

Selector for element to stop at.

AcceptedFilters<Element> | null

filterSelector?​

AcceptedFilters<Element>

Optional filter for parents.

Returns​

Cheerio<Element>

The parents.

Example​

$('.orange').parentsUntil('#food').length;
//=> 1

See​

https://api.jquery.com/parentsUntil/


prev()​

prev: <T>(this, selector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:475

Gets the previous sibling of each selected element optionally filtered by a selector.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<Element>

If specified filter for siblings.

Returns​

Cheerio<Element>

The previous nodes.

Example​

$('.orange').prev().hasClass('apple');
//=> true

See​

https://api.jquery.com/prev/


prevAll()​

prevAll: <T>(this, selector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:499

Gets all the preceding siblings of each selected element, optionally filtered by a selector.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<Element>

If specified filter for siblings.

Returns​

Cheerio<Element>

The previous nodes.

Example​

$('.pear').prevAll();
//=> [<li class="orange">Orange</li>, <li class="apple">Apple</li>]

$('.pear').prevAll('.orange');
//=> [<li class="orange">Orange</li>]

See​

https://api.jquery.com/prevAll/


prevUntil()​

prevUntil: <T>(this, selector?, filterSelector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:528

Gets all the preceding siblings up to but not including the element matched by the selector, optionally filtered by another selector.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

Selector for element to stop at.

AcceptedFilters<Element> | null

filterSelector?​

AcceptedFilters<Element>

If specified filter for siblings.

Returns​

Cheerio<Element>

The previous nodes.

Example​

$('.pear').prevUntil('.apple');
//=> [<li class="orange">Orange</li>]

See​

https://api.jquery.com/prevUntil/


siblings()​

siblings: <T>(this, selector?) => Cheerio<Element>

Defined in: src/api/traversing.ts:556

Get the siblings of each element (excluding the element) in the set of matched elements, optionally filtered by a selector.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<Element>

If specified filter for siblings.

Returns​

Cheerio<Element>

The siblings.

Example​

$('.pear').siblings().length;
//=> 2

$('.pear').siblings('.orange').length;
//=> 1

See​

https://api.jquery.com/siblings/


add()​

add<S, T>(this, other, context?): Cheerio<S | T>

Defined in: src/api/traversing.ts:1140

Add elements to the set of matched elements.

Type Parameters​

S​

S extends AnyNode

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

other​

Elements to add.

string | S | Cheerio<S> | S[]

context?​

Optionally the context of the new selection.

string | Cheerio<S>

Returns​

Cheerio<S | T>

The combined set.

Example​

$('.apple').add('.orange').length;
//=> 2

See​

https://api.jquery.com/add/


addBack()​

addBack<T>(this, selector?): Cheerio<AnyNode>

Defined in: src/api/traversing.ts:1166

Add the previous set of elements on the stack to the current set, optionally filtered by a selector.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

string

Selector for the elements to add.

Returns​

Cheerio<AnyNode>

The combined set.

Example​

$('li').eq(0).addBack('.orange').length;
//=> 2

See​

https://api.jquery.com/addBack/


closest()​

closest<T>(this, selector?): Cheerio<AnyNode>

Defined in: src/api/traversing.ts:343

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.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<Element>

Selector for the element to find.

Returns​

Cheerio<AnyNode>

The closest nodes.

Example​

$('.orange').closest();
//=> []

$('.orange').closest('.apple');
// => []

$('.orange').closest('li');
//=> [<li class="orange">Orange</li>]

$('.orange').closest('#fruits');
//=> [<ul id="fruits"> ... </ul>]

See​

https://api.jquery.com/closest/


contents()​

contents<T>(this): Cheerio<AnyNode>

Defined in: src/api/traversing.ts:606

Gets the children of each element in the set of matched elements, including text and comment nodes.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

Returns​

Cheerio<AnyNode>

The children.

Example​

$('#fruits').contents().length;
//=> 3

See​

https://api.jquery.com/contents/


each()​

each<T>(this, fn): Cheerio<T>

Defined in: src/api/traversing.ts:642

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.

Type Parameters​

T​

T

Parameters​

this​

Cheerio<T>

fn​

(this, i, el) => boolean | void

Function to execute.

Returns​

Cheerio<T>

The instance itself, useful for chaining.

Example​

const fruits = [];

$('li').each(function (i, elem) {
fruits[i] = $(this).text();
});

fruits.join(', ');
//=> Apple, Orange, Pear

See​

https://api.jquery.com/each/


end()​

end<T>(this): Cheerio<AnyNode>

Defined in: src/api/traversing.ts:1120

End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.

Type Parameters​

T​

T

Parameters​

this​

Cheerio<T>

Returns​

Cheerio<AnyNode>

The previous state of the set of matched elements.

Example​

$('li').eq(0).end().length;
//=> 3

See​

https://api.jquery.com/end/


eq()​

eq<T>(this, i): Cheerio<T>

Defined in: src/api/traversing.ts:968

Reduce the set of matched elements to the one at the specified index. Use .eq(-i) to count backwards from the last selected element.

Type Parameters​

T​

T

Parameters​

this​

Cheerio<T>

i​

number

Index of the element to select.

Returns​

Cheerio<T>

The element at the ith position.

Example​

$('li').eq(0).text();
//=> Apple

$('li').eq(-1).text();
//=> Pear

See​

https://api.jquery.com/eq/


filter()​

Call Signature​

filter<T, S>(this, match): Cheerio<S>

Defined in: src/api/traversing.ts:737

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.

Type Parameters​
T​

T

S​

S

Parameters​
this​

Cheerio<T>

match​

(this, index, value) => value is S

Value to look for, following the rules above.

Returns​

Cheerio<S>

The filtered collection.

Example​
$('li')
.filter(function (i, el) {
// this === el
return $(this).attr('class') === 'orange';
})
.attr('class'); //=> orange
See​

https://api.jquery.com/filter/

Call Signature​

filter<T, S>(this, match): Cheerio<S extends string ? Element : T>

Defined in: src/api/traversing.ts:776

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.
Type Parameters​
T​

T

S​

S

Parameters​
this​

Cheerio<T>

match​

S

Value to look for, following the rules above. See AcceptedFilters.

Returns​

Cheerio<S extends string ? Element : T>

The filtered collection.

Examples​
$('li').filter('.orange').attr('class');
//=> orange
$('li')
.filter(function (i, el) {
// this === el
return $(this).attr('class') === 'orange';
})
.attr('class'); //=> orange
See​

https://api.jquery.com/filter/


find()​

find<T>(this, selectorOrHaystack?): Cheerio<Element>

Defined in: src/api/traversing.ts:47

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selectorOrHaystack?​

Element to look for.

string | Element | Cheerio<Element>

Returns​

Cheerio<Element>

The found elements.

Example​

$('#fruits').find('li').length;
//=> 3
$('#fruits').find($('.apple')).length;
//=> 1

See​

https://api.jquery.com/find/


first()​

first<T>(this): Cheerio<T>

Defined in: src/api/traversing.ts:927

Will select the first element of a cheerio object.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

Returns​

Cheerio<T>

The first element.

Example​

$('#fruits').children().first().text();
//=> Apple

See​

https://api.jquery.com/first/


get()​

Call Signature​

get<T>(this, i): undefined | T

Defined in: src/api/traversing.ts:994

Retrieve one of the elements matched by the Cheerio object, at the ith position.

Type Parameters​
T​

T

Parameters​
this​

Cheerio<T>

i​

number

Element to retrieve.

Returns​

undefined | T

The element at the ith position.

Example​
$('li').get(0).tagName;
//=> li
See​

https://api.jquery.com/get/

Call Signature​

get<T>(this): T[]

Defined in: src/api/traversing.ts:1009

Retrieve all elements matched by the Cheerio object, as an array.

Type Parameters​
T​

T

Parameters​
this​

Cheerio<T>

Returns​

T[]

All elements matched by the Cheerio object.

Example​
$('li').get().length;
//=> 3
See​

https://api.jquery.com/get/


has()​

has(this, selectorOrHaystack): Cheerio<AnyNode>

Defined in: src/api/traversing.ts:901

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)').

Parameters​

this​

Cheerio<AnyNode>

selectorOrHaystack​

Element to look for.

string | Element | Cheerio<Element>

Returns​

Cheerio<AnyNode>

The filtered collection.

Examples​

$('ul').has('.pear').attr('id');
//=> fruits
$('ul').has($('.pear')[0]).attr('id');
//=> fruits

See​

https://api.jquery.com/has/


index()​

index<T>(this, selectorOrNeedle?): number

Defined in: src/api/traversing.ts:1051

Search for a given element from among the matched elements.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

selectorOrNeedle?​

Element to look for.

string | AnyNode | Cheerio<AnyNode>

Returns​

number

The index of the element.

Example​

$('.pear').index();
//=> 2 $('.orange').index('li');
//=> 1
$('.apple').index($('#fruit, li'));
//=> 1

See​

https://api.jquery.com/index/


is()​

is<T>(this, selector?): boolean

Defined in: src/api/traversing.ts:812

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.

Type Parameters​

T​

T

Parameters​

this​

Cheerio<T>

selector?​

AcceptedFilters<T>

Selector for the selection.

Returns​

boolean

Whether or not the selector matches an element of the instance.

See​

https://api.jquery.com/is/


last()​

last<T>(this): Cheerio<T>

Defined in: src/api/traversing.ts:945

Will select the last element of a cheerio object.

Type Parameters​

T​

T

Parameters​

this​

Cheerio<T>

Returns​

Cheerio<T>

The last element.

Example​

$('#fruits').children().last().text();
//=> Pear

See​

https://api.jquery.com/last/


map()​

map<T, M>(this, fn): Cheerio<M>

Defined in: src/api/traversing.ts:678

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.

Type Parameters​

T​

T

M​

M

Parameters​

this​

Cheerio<T>

fn​

(this, i, el) => undefined | null | M | M[]

Function to execute.

Returns​

Cheerio<M>

The mapped elements, wrapped in a Cheerio collection.

Example​

$('li')
.map(function (i, el) {
// this === el
return $(this).text();
})
.toArray()
.join(' ');
//=> "apple orange pear"

See​

https://api.jquery.com/map/


not()​

not<T>(this, match): Cheerio<T>

Defined in: src/api/traversing.ts:860

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.

Type Parameters​

T​

T extends AnyNode

Parameters​

this​

Cheerio<T>

match​

AcceptedFilters<T>

Value to look for, following the rules above.

Returns​

Cheerio<T>

The filtered collection.

Examples​

$('li').not('.apple').length;
//=> 2
$('li').not(function (i, el) {
// this === el
return $(this).attr('class') === 'orange';
}).length; //=> 2

See​

https://api.jquery.com/not/


slice()​

slice<T>(this, start?, end?): Cheerio<T>

Defined in: src/api/traversing.ts:1097

Gets the elements matching the specified range (0-based position).

Type Parameters​

T​

T

Parameters​

this​

Cheerio<T>

start?​

number

A position at which the elements begin to be selected. If negative, it indicates an offset from the end of the set.

end?​

number

A 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.

Returns​

Cheerio<T>

The elements matching the specified range.

Example​

$('li').slice(1).eq(0).text();
//=> 'Orange'

$('li').slice(1, 2).length;
//=> 1

See​

https://api.jquery.com/slice/