Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Cheerio<T> Abstract

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

  • 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

_root: null | Cheerio<Document>

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

cheerio: "[cheerio object]"
length: number = 0
options: InternalOptions
prevObject: undefined | Cheerio<any>
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

  • 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

    Returns Cheerio<T>

  • 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

    Returns Cheerio<T>

    The instance itself, for chaining.

Traversing Functions

Attributes Methods

  • addClass<T, R>(this: R, value?: string | ((this: Element, 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

    Parameters

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

      Name of new class.

    Returns R

    The instance itself.

  • attr<T>(this: Cheerio<T>, name: string): string | undefined
  • attr<T>(this: Cheerio<T>): Record<string, string> | undefined
  • attr<T>(this: Cheerio<T>, name: string, value?: null | string | ((this: Element, i: number, attrib: string) => null | string)): Cheerio<T>
  • attr<T>(this: Cheerio<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

    • this: Cheerio<T>
    • 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

    Parameters

    Returns Record<string, string> | undefined

    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

    • this: Cheerio<T>
    • name: string

      Name of the attribute.

    • Optional value: null | string | ((this: Element, 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

    • this: Cheerio<T>
    • values: Record<string, null | string>

      Map of attribute names and values.

    Returns Cheerio<T>

    The instance itself.

  • data<T>(this: Cheerio<T>, name: string): unknown | undefined
  • data<T>(this: Cheerio<T>): Record<string, unknown>
  • data<T>(this: Cheerio<T>, name: string, value: unknown): Cheerio<T>
  • data<T>(this: Cheerio<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

    • this: Cheerio<T>
    • name: string

      Name of the data attribute.

    Returns unknown | undefined

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

  • 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

    Parameters

    Returns Record<string, unknown>

    A map with all of the data attributes.

  • 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

    • this: Cheerio<T>
    • 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

    • this: Cheerio<T>
    • values: Record<string, unknown>

      Map of names to values.

    Returns Cheerio<T>

    The instance itself.

  • hasClass<T>(this: Cheerio<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

    • this: Cheerio<T>
    • className: string

      Name of the class.

    Returns boolean

    Indicates if an element has the given className.

  • 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<T>(this: Cheerio<T>, name: "tagName" | "nodeName"): T extends Element ? string : undefined
  • prop<T>(this: Cheerio<T>, name: "innerHTML" | "outerHTML" | "innerText" | "textContent"): string | null
  • prop<T>(this: Cheerio<T>, name: "style"): StyleProp
  • prop<T>(this: Cheerio<T>, name: "href" | "src"): string | undefined
  • prop<T, K>(this: Cheerio<T>, name: K): Element[K]
  • prop<T, K>(this: Cheerio<T>, name: K, value: Element[K] | ((this: Element, i: number, prop: K) => undefined | null | string | number | Document | Element | CDATA | Text | Comment | ProcessingInstruction | Record<string, string> | {} | TagSourceCodeLocation | Attribute[] | ChildNode[] | (<T>(this: T, recursive?: boolean) => T))): Cheerio<T>
  • prop<T>(this: Cheerio<T>, name: Record<string, undefined | null | string | number | boolean | Document | Element | CDATA | Text | Comment | ProcessingInstruction | Record<string, string> | {} | TagSourceCodeLocation | Attribute[] | ChildNode[] | (<T>(this: T, recursive?: boolean) => T)>): Cheerio<T>
  • prop<T>(this: Cheerio<T>, name: string, value: null | string | boolean | ((this: Element, i: number, prop: string) => string | boolean)): Cheerio<T>
  • prop<T>(this: Cheerio<T>, name: string): string
  • Method for getting and setting properties. Gets the property value for only the first element in the matched set.

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

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

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

    Type parameters

    Parameters

    • this: Cheerio<T>
    • name: "tagName" | "nodeName"

      Name of the property.

    Returns T extends Element ? string : undefined

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

  • Type parameters

    Parameters

    • this: Cheerio<T>
    • name: "innerHTML" | "outerHTML" | "innerText" | "textContent"

    Returns string | null

  • Get a parsed CSS style object.

    Type parameters

    Parameters

    Returns StyleProp

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

    example

    With baseURI set to 'https://example.com':

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

    Type parameters

    Parameters

    • this: Cheerio<T>
    • name: "href" | "src"

    Returns string | undefined

  • Get a property of an element.

    Type parameters

    Parameters

    Returns Element[K]

  • Set a property of an element.

    Type parameters

    Parameters

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

    Returns Cheerio<T>

  • Type parameters

    Parameters

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

    Returns Cheerio<T>

  • Type parameters

    Parameters

    • this: Cheerio<T>
    • name: string
    • value: null | string | boolean | ((this: Element, i: number, prop: string) => string | boolean)

    Returns Cheerio<T>

  • Type parameters

    Parameters

    Returns string

  • 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

    • this: Cheerio<T>
    • name: string

      Name of the attribute.

    Returns Cheerio<T>

    The instance itself.

  • removeClass<T, R>(this: R, name?: string | ((this: Element, 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

    Parameters

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

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

    Returns R

    The instance itself.

  • toggleClass<T, R>(this: R, value?: string | ((this: Element, 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

    Parameters

    • this: R
    • Optional value: string | ((this: Element, 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<T>(this: Cheerio<T>): string | undefined | string[]
  • val<T>(this: Cheerio<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

    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

    • this: Cheerio<T>
    • value: string | string[]

      The new value.

    Returns Cheerio<T>

    The instance itself.

CSS Methods

  • css<T>(this: Cheerio<T>, names?: string[]): Record<string, string> | undefined
  • css<T>(this: Cheerio<T>, name: string): string | undefined
  • css<T>(this: Cheerio<T>, prop: string, val: string | ((this: Element, i: number, style: string) => undefined | string)): Cheerio<T>
  • css<T>(this: Cheerio<T>, map: Record<string, string>): Cheerio<T>

Forms Methods

  • serialize<T>(this: Cheerio<T>): string
  • serializeArray<T>(this: Cheerio<T>): SerializedField[]

Manipulation Methods

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

  • Gets an HTML content string from the first selected element.

    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

    Parameters

    Returns string | null

    The HTML content string.

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

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

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

    Type parameters

    Parameters

    • this: Cheerio<T>
    • str: string | Cheerio<T>

      The content to replace selection's contents with.

    Returns Cheerio<T>

    The instance itself.

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

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

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

  • 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

    • this: Cheerio<T>
    • Optional selector: string

      Optional selector for elements to remove.

    Returns Cheerio<T>

    The instance itself.

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

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

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

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

    Type parameters

    Parameters

    Returns string

    The text contents of the collection.

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

    example
    $('.orange').text('Orange');
    //=> <div class="orange">Orange</div>
    see

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

    Type parameters

    Parameters

    • this: Cheerio<T>
    • str: string | ((this: AnyNode, i: number, text: string) => string)

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

    Returns Cheerio<T>

    The instance itself.

  • toString<T>(this: Cheerio<T>): string
  • 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

    • this: Cheerio<T>
    • 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.

  • 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

    • this: Cheerio<T>
    • 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<T, any, undefined>
  • Returns Iterator<T, any, undefined>

  • Make a cheerio object.

    Type parameters

    • T

    Parameters

    • dom: string | T | ArrayLike<T>

      The contents of the new object.

    • Optional context: BasicAcceptedElems<AnyNode>

      The context of the new object.

    Returns Cheerio<T>

    The new cheerio object.

  • Parses some content.

    Parameters

    • content: string | AnyNode | AnyNode[] | Buffer

      Content to parse.

    • options: InternalOptions

      Options for parsing.

    • isDocument: boolean

      Allows parser to be switched to fragment mode.

    • context: null | ParentNode

    Returns Document

    A document containing the content.

  • Render an element or a set of elements.

    Parameters

    Returns string

    The rendered DOM.

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

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

    Type parameters

    • T

    Parameters

    Returns T[]

    The contained items.

Traversing Methods

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

    The closest nodes.

  • each<T>(this: Cheerio<T>, fn: (this: T, 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

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

      Function to execute.

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

          • this: T
          • i: number
          • el: T

          Returns boolean | void

    Returns Cheerio<T>

    The instance itself, useful for chaining.

  • 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

    • this: Cheerio<T>
    • i: number

      Index of the element to select.

    Returns Cheerio<T>

    The element at the ith position.

  • filter<T, S>(this: Cheerio<T>, match: (this: T, index: number, value: T) => value is S): Cheerio<S>
  • filter<T, S>(this: Cheerio<T>, 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

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

      Value to look for, following the rules above.

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

          • this: T
          • 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

    Returns Cheerio<S extends string ? Element : T>

    The filtered collection.

  • get<T>(this: Cheerio<T>, i: number): T | undefined
  • get<T>(this: Cheerio<T>): T[]
  • 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<AnyNode | Element>

    The filtered collection.

  • map<T, M>(this: Cheerio<T>, fn: (this: T, 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

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

      Function to execute.

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

          • this: T
          • i: number
          • el: T

          Returns undefined | null | M | M[]

    Returns Cheerio<M>

    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.

    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.

  • 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

    • this: Cheerio<T>
    • 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.