Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Cheerio<T>

The main types of Cheerio objects.

Type parameters

  • T

Hierarchy

  • AttributesType
  • TraversingType
  • ManipulationType
  • CssType
  • FormsType
  • Iterable<T>
    • Cheerio

Implements

  • ArrayLike<T>

Indexable

[index: number]: T

Index

Constructors

Private constructor

  • new Cheerio<T>(elements: undefined | ArrayLike<T>, root: null | Cheerio<Document>, options: InternalOptions): Cheerio<T>
  • Instance of cheerio. Methods are specified in the modules. Usage of this constructor is not recommended. Please use $.load instead.

    Type parameters

    • T

    Parameters

    • elements: undefined | ArrayLike<T>

      The new selection.

    • root: null | Cheerio<Document>

      Sets the root node.

    • options: InternalOptions

      Options for the instance.

    Returns Cheerio<T>

Properties

Private _root

_root: null | Cheerio<Document>

The root of the document. Can be set by using the root argument of the constructor.

cheerio

cheerio: "[cheerio object]"

length

length: number = 0

options

options: InternalOptions

prevObject

prevObject: undefined | Cheerio<any>

splice

splice: (start?: number, end?: number) => any[]

Type declaration

    • (start?: number, end?: number): any[]
    • Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.

      Parameters

      • Optional start: number

        The beginning index of the specified portion of the array. If start is undefined, then the slice begins at index 0.

      • Optional end: number

        The end index of the specified portion of the array. This is exclusive of the element at the index 'end'. If end is undefined, then the slice extends to the end of the array.

      Returns any[]

Manipulation Functions

Const append

Const prepend

Const wrap

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

    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/

    Type parameters

    Parameters

    • wrapper: AcceptedElems<Node>

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

    Returns Cheerio<T>

Const wrapInner

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

    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/

    Type parameters

    Parameters

    • wrapper: AcceptedElems<Node>

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

    Returns Cheerio<T>

    The instance itself, for chaining.

Traversing Functions

Const children

Const next

Const nextAll

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

    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/

    Type parameters

    Parameters

    Returns Cheerio<Element>

    The next nodes.

Const nextUntil

Const parent

Const parents

Const parentsUntil

Const prev

Const prevAll

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

    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/

    Type parameters

    Parameters

    Returns Cheerio<Element>

    The previous nodes.

Const prevUntil

Const siblings

Attributes Methods

addClass

  • addClass<T, R>(value?: string | ((i: number, className: string) => undefined | string)): R
  • Adds class(es) to all of the matched elements. Also accepts a function.

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

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

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

    Type parameters

    • T: Node<T>

    • R: ArrayLike<T, R>

    Parameters

    • Optional value: string | ((i: number, className: string) => undefined | string)

      Name of new class.

    Returns R

    The instance itself.

attr

  • attr<T>(name: string): string | undefined
  • attr<T>(): Record<string, string>
  • attr<T>(name: string, value?: null | string | ((i: number, attrib: string) => null | string)): Cheerio<T>
  • attr<T>(values: Record<string, null | string>): Cheerio<T>
  • Method for getting attributes. Gets the attribute value for only the first element in the matched set.

    example
    $('ul').attr('id');
    //=> fruits
    see

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

    Type parameters

    Parameters

    • name: string

      Name of the attribute.

    Returns string | undefined

    The attribute's value.

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

    example
    $('ul').attr();
    //=> { id: 'fruits' }
    see

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

    Type parameters

    Returns Record<string, string>

    The attribute's values.

  • Method for setting attributes. Sets 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.

    example
    $('.apple').attr('id', 'favorite').html();
    //=> <li class="apple" id="favorite">Apple</li>
    see

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

    Type parameters

    Parameters

    • name: string

      Name of the attribute.

    • Optional value: null | string | ((i: number, attrib: string) => null | string)

      The new value of the attribute.

    Returns Cheerio<T>

    The instance itself.

  • Method for setting multiple attributes at once. Sets 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.

    example
    $('.apple').attr({ id: 'favorite' }).html();
    //=> <li class="apple" id="favorite">Apple</li>
    see

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

    Type parameters

    Parameters

    • values: Record<string, null | string>

      Map of attribute names and values.

    Returns Cheerio<T>

    The instance itself.

data

  • data<T>(name: string): unknown | undefined
  • data<T>(): Record<string, unknown>
  • data<T>(name: string, value: unknown): Cheerio<T>
  • data<T>(values: Record<string, unknown>): Cheerio<T>
  • Method for getting data attributes, for only the first element in the matched set.

    example
    $('<div data-apple-color="red"></div>').data('apple-color');
    //=> 'red'
    see

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

    Type parameters

    Parameters

    • name: string

      Name of the data attribute.

    Returns unknown | undefined

    The data attribute's value.

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

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

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

    Type parameters

    Returns Record<string, unknown>

    The data attribute's values.

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

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

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

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

    Type parameters

    Parameters

    • name: string

      Name of the data attribute.

    • value: unknown

      The new value.

    Returns Cheerio<T>

    The instance itself.

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

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

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

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

    Type parameters

    Parameters

    • values: Record<string, unknown>

      Map of names to values.

    Returns Cheerio<T>

    The instance itself.

hasClass

  • hasClass<T>(className: string): boolean
  • Check to see if any of the matched elements have the given className.

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

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

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

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

    Type parameters

    Parameters

    • className: string

      Name of the class.

    Returns boolean

    Indicates if an element has the given className.

is

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

    see

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

    Type parameters

    • T

    Parameters

    Returns boolean

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

prop

  • prop<T>(name: "tagName" | "nodeName"): T extends Element ? string : undefined
  • prop<T>(name: "innerHTML" | "outerHTML"): string | null
  • prop<T>(name: "style"): StyleProp
  • prop<T, K>(name: K): Element[K]
  • prop<T, K>(name: K, value: Element[K] | ((i: number, prop: K) => undefined | null | string | number | Node | Node[] | Record<string, string> | NodeWithChildren | {} | Attribute[] | (<T>(recursive?: boolean) => T))): Cheerio<T>
  • prop<T>(name: Record<string, undefined | null | string | number | boolean | Node | Node[] | Record<string, string> | NodeWithChildren | {} | Attribute[] | (<T>(recursive?: boolean) => T)>): Cheerio<T>
  • prop<T>(name: string, value: null | string | boolean | ((i: number, prop: string) => string | boolean)): Cheerio<T>
  • prop<T>(name: string): string

removeAttr

  • removeAttr<T>(name: string): Cheerio<T>
  • Method for removing attributes by name.

    example
    $('.pear').removeAttr('class').html();
    //=> <li>Pear</li>

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

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

    Type parameters

    Parameters

    • name: string

      Name of the attribute.

    Returns Cheerio<T>

    The instance itself.

removeClass

  • removeClass<T, R>(name?: string | ((i: number, className: string) => undefined | string)): R
  • 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.

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

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

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

    Type parameters

    • T: Node<T>

    • R: ArrayLike<T, R>

    Parameters

    • Optional name: string | ((i: number, className: string) => undefined | string)

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

    Returns R

    The instance itself.

toggleClass

  • toggleClass<T, R>(value?: string | ((i: number, className: string, stateVal?: boolean) => string), stateVal?: boolean): R
  • 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.

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

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

    Type parameters

    • T: Node<T>

    • R: ArrayLike<T, R>

    Parameters

    • Optional value: string | ((i: number, className: string, stateVal?: boolean) => string)

      Name of the class. Can also be a function.

    • Optional stateVal: boolean

      If specified the state of the class.

    Returns R

    The instance itself.

val

  • val<T>(): string | undefined | string[]
  • val<T>(value: string | string[]): Cheerio<T>
  • Method for getting the value of input, select, and textarea. Note: Support for map, and function has not been added yet.

    example
    $('input[type="text"]').val();
    //=> input_text
    see

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

    Type parameters

    Returns string | undefined | string[]

    The value.

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

    example
    $('input[type="text"]').val('test').html();
    //=> <input type="text" value="test"/>
    see

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

    Type parameters

    Parameters

    • value: string | string[]

      The new value.

    Returns Cheerio<T>

    The instance itself.

CSS Methods

css

  • css<T>(names?: string[]): Record<string, string>
  • css<T>(name: string): string | undefined
  • css<T>(prop: string, val: string | ((i: number, style: string) => undefined | string)): Cheerio<T>
  • css<T>(prop: Record<string, string>): Cheerio<T>
  • Get the value of a style property for the first element in the set of matched elements.

    see

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

    Type parameters

    Parameters

    • Optional names: string[]

      Optionally the names of the property of interest.

    Returns Record<string, string>

    A map of all of the style properties.

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

    see

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

    Type parameters

    Parameters

    • name: string

    Returns string | undefined

    The property value for the given name.

  • Set one CSS property for every matched element.

    see

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

    Type parameters

    Parameters

    • prop: string

      The name of the property.

    • val: string | ((i: number, style: string) => undefined | string)

      The new value.

    Returns Cheerio<T>

    The instance itself.

  • Set multiple CSS properties for every matched element.

    see

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

    Type parameters

    Parameters

    • prop: Record<string, string>

      The name of the property.

    Returns Cheerio<T>

    The instance itself.

Forms Methods

serialize

  • serialize<T>(): string

serializeArray

  • serializeArray<T>(): SerializedField[]
  • Encode a set of form elements as an array of names and values.

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

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

    Type parameters

    Returns SerializedField[]

    The serialized form.

Manipulation Methods

Private _makeDomArray

  • Create an array of nodes, recursing into arrays and parsing strings if necessary.

    Type parameters

    Parameters

    • Optional elem: BasicAcceptedElems<Node>

      Elements to make an array of.

    • Optional clone: boolean

      Optionally clone nodes.

    Returns Node[]

    The array of nodes.

after

appendTo

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

    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/

    Type parameters

    Parameters

    Returns Cheerio<T>

    The instance itself.

before

clone

empty

html

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

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

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

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

    Type parameters

    Returns string | null

    The instance itself.

  • Type parameters

    Parameters

    Returns Cheerio<T>

insertAfter

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

    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/

    Type parameters

    Parameters

    Returns Cheerio<T>

    The set of newly inserted elements.

insertBefore

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

    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/

    Type parameters

    Parameters

    Returns Cheerio<T>

    The set of newly inserted elements.

prependTo

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

    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/

    Type parameters

    Parameters

    Returns Cheerio<T>

    The instance itself.

remove

  • remove<T>(selector?: string): Cheerio<T>
  • Removes the set of matched elements from the DOM and all their children. selector filters the set of matched elements to be removed.

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

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

    Type parameters

    Parameters

    • Optional selector: string

      Optional selector for elements to remove.

    Returns Cheerio<T>

    The instance itself.

replaceWith

  • Replaces matched elements with content.

    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/

    Type parameters

    Parameters

    Returns Cheerio<T>

    The instance itself.

text

  • text<T>(): string
  • text<T>(str: string | ((i: number, text: string) => string)): Cheerio<T>
  • 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.

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

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

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

    Type parameters

    Returns string

    The instance itself when setting text, otherwise the rendered document.

  • Type parameters

    Parameters

    • str: string | ((i: number, text: string) => string)

    Returns Cheerio<T>

toString

  • toString<T>(): string

unwrap

  • unwrap<T>(selector?: string): Cheerio<T>
  • The .unwrap() function, removes the parents of the set of matched elements from the DOM, leaving the matched elements in their place.

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

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

    Type parameters

    Parameters

    • Optional 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.

wrapAll

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

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

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

    Type parameters

    Parameters

    • wrapper: AcceptedElems<T>

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

    Returns Cheerio<T>

    The instance itself.

Other Methods

[iterator]

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

Abstract Private _make

  • Make a cheerio object.

    Type parameters

    • T

    Parameters

    • dom: string | T | ArrayLike<T>

      The contents of the new object.

    • Optional context: BasicAcceptedElems<Node>

      The context of the new object.

    Returns Cheerio<T>

    The new cheerio object.

Abstract Private _parse

  • Parses some content.

    Parameters

    • content: string | Node | Node[] | Buffer | Document

      Content to parse.

    • options: InternalOptions

      Options for parsing.

    • isDocument: boolean

      Allows parser to be switched to fragment mode.

    Returns Document

    A document containing the content.

Abstract Private _render

  • _render(dom: Node | ArrayLike<Node>): string
  • Render an element or a set of elements.

    Parameters

    • dom: Node | ArrayLike<Node>

      DOM to render.

    Returns string

    The rendered DOM.

filterArray

toArray

  • toArray<T>(): T[]
  • Retrieve all the DOM elements contained in the jQuery set as an array.

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

    Type parameters

    • T

    Returns T[]

    The contained items.

Traversing Methods

add

addBack

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

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

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

    Type parameters

    Parameters

    • Optional selector: string

      Selector for the elements to add.

    Returns Cheerio<Node>

    The combined set.

closest

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

    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/

    Type parameters

    Parameters

    Returns Cheerio<Node>

    The closest nodes.

contents

each

  • each<T>(fn: (i: number, el: T) => boolean | void): Cheerio<T>
  • 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.

    example
    const fruits = [];

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

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

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

    Type parameters

    • T

    Parameters

    • fn: (i: number, el: T) => boolean | void

      Function to execute.

        • (i: number, el: T): boolean | void
        • Parameters

          • i: number
          • el: T

          Returns boolean | void

    Returns Cheerio<T>

    The instance itself, useful for chaining.

end

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

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

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

    Type parameters

    • T

    Returns Cheerio<Node>

    The previous state of the set of matched elements.

eq

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

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

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

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

    Type parameters

    • T

    Parameters

    • i: number

      Index of the element to select.

    Returns Cheerio<T>

    The element at the ith position.

filter

  • filter<T, S>(match: (index: number, value: T) => value is S): Cheerio<S>
  • filter<T, S>(match: S): Cheerio<S extends string ? Element : T>
  • 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.

    example
    Function
    $('li')
    .filter(function (i, el) {
    // this === el
    return $(this).attr('class') === 'orange';
    })
    .attr('class'); //=> orange
    see

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

    Type parameters

    • T

    • S

    Parameters

    • match: (index: number, value: T) => value is S

      Value to look for, following the rules above.

        • (index: number, value: T): value is S
        • Parameters

          • index: number
          • value: T

          Returns value is S

    Returns Cheerio<S>

    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.

    • 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.
    example
    Selector
    $('li').filter('.orange').attr('class');
    //=> orange
    example
    Function
    $('li')
    .filter(function (i, el) {
    // this === el
    return $(this).attr('class') === 'orange';
    })
    .attr('class'); //=> orange
    see

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

    Type parameters

    • T

    • S

    Parameters

    • match: S

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

    Returns Cheerio<S extends string ? Element : T>

    The filtered collection.

find

first

get

  • get<T>(i: number): T
  • get<T>(): T[]
  • Retrieve one of the elements matched by the Cheerio object, at the ith position.

    example
    $('li').get(0).tagName;
    //=> li
    see

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

    Type parameters

    • T

    Parameters

    • i: number

      Element to retrieve.

    Returns T

    The element at the ith position.

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

    example
    $('li').get().length;
    //=> 3
    see

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

    Type parameters

    • T

    Returns T[]

    All elements matched by the Cheerio object.

has

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

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

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

    Parameters

    Returns Cheerio<Node | Element>

    The filtered collection.

index

  • Search for a given element from among the matched elements.

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

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

    Type parameters

    Parameters

    • Optional selectorOrNeedle: string | Node | Cheerio<Node>

      Element to look for.

    Returns number

    The index of the element.

last

map

  • map<T, M>(fn: (i: number, el: T) => undefined | null | M | M[]): Cheerio<M>
  • 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.

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

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

    Type parameters

    • T

    • M

    Parameters

    • fn: (i: number, el: T) => undefined | null | M | M[]

      Function to execute.

        • (i: number, el: T): undefined | null | M | M[]
        • Parameters

          • i: number
          • el: T

          Returns undefined | null | M | M[]

    Returns Cheerio<M>

    The mapped elements, wrapped in a Cheerio collection.

not

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

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

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

    Type parameters

    Parameters

    Returns Cheerio<T>

    The filtered collection.

slice

  • slice<T>(start?: number, end?: number): Cheerio<T>
  • Gets the elements matching the specified range (0-based position).

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

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

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

    Type parameters

    • T

    Parameters

    • Optional start: number

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

    • Optional end: number

      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.

    Returns Cheerio<T>

    The elements matching the specified range.