Trilium Backend API
    Preparing search index...

    Class Cheerio<T>Abstract

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

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

    Type Parameters

    • T

    Hierarchy (View Summary)

    Implements

    Indexable

    • [index: number]: T
    Index

    Attributes

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

      Type Parameters

      Parameters

      • this: R
      • Optionalvalue: string | ((this: Element, i: number, className: string) => string)

        Name of new class.

      Returns R

      The instance itself.

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

      $('.apple').addClass('fruit red').prop('outerHTML');
      //=> <li class="apple fruit red">Apple</li>
    • Method for getting attributes. Gets the attribute value for only the first element in the matched set.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • name: string

        Name of the attribute.

      Returns string

      The attribute's value.

      $('ul').attr('id');
      //=> fruits
    • Method for getting all attributes and their values of the first element in the matched set.

      Type Parameters

      Parameters

      Returns Record<string, string>

      The attribute's values.

      $('ul').attr();
      //=> { id: 'fruits' }
    • 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

      Parameters

      • this: Cheerio<T>
      • name: string

        Name of the attribute.

      • Optionalvalue: string | ((this: Element, i: number, attrib: string) => string)

        The new value of the attribute.

      Returns Cheerio<T>

      The instance itself.

      $('.apple').attr('id', 'favorite').prop('outerHTML');
      //=> <li class="apple" id="favorite">Apple</li>
    • 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

      Parameters

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

        Map of attribute names and values.

      Returns Cheerio<T>

      The instance itself.

      $('.apple').attr({ id: 'favorite' }).prop('outerHTML');
      //=> <li class="apple" id="favorite">Apple</li>
    • Method for getting data attributes, for only the first element in the matched set.

      Type Parameters

      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.

      $('<div data-apple-color="red"></div>').data('apple-color');
      //=> 'red'
    • Method for getting all of an element's data attributes, for only the first element in the matched set.

      Type Parameters

      Parameters

      Returns Record<string, unknown>

      A map with all of the data attributes.

      $('<div data-apple-color="red"></div>').data();
      //=> { appleColor: 'red' }
    • Method for setting data attributes, for only the first element in the matched set.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • name: string

        Name of the data attribute.

      • value: unknown

        The new value.

      Returns Cheerio<T>

      The instance itself.

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

      apple.data('kind');
      //=> 'mac'
    • Method for setting multiple data attributes at once, for only the first element in the matched set.

      Type Parameters

      Parameters

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

        Map of names to values.

      Returns Cheerio<T>

      The instance itself.

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

      apple.data('kind');
      //=> 'mac'
    • Check to see if any of the matched elements have the given className.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • className: string

        Name of the class.

      Returns boolean

      Indicates if an element has the given className.

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

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

      $('li').hasClass('pear');
      //=> true
    • Method for getting and setting properties. Gets the property value for only the first element in the matched set.

      Type Parameters

      Parameters

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

        Name of the property.

      Returns string

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

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

      $('input[type="checkbox"]').prop('checked', true).val();
      //=> ok
    • Method for getting and setting properties. Gets the property value for only the first element in the matched set.

      Type Parameters

      Parameters

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

        Name of the property.

      Returns string

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

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

      $('input[type="checkbox"]').prop('checked', true).val();
      //=> ok
    • Get a parsed CSS style object.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • name: "style"

        Name of the property.

      Returns StyleProp

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

    • 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

      Parameters

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

        Name of the property.

      Returns string

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

      $('<img src="image.png">').prop('src');
      //=> 'https://example.com/image.png'
    • Get a property of an element.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • name: K

        Name of the property.

      Returns Element[K]

      The property's value.

    • Set a property of an element.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • name: K

        Name of the property.

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

        Value to set the property to.

      Returns Cheerio<T>

      The instance itself.

    • Set multiple properties of an element.

      Type Parameters

      Parameters

      Returns Cheerio<T>

      The instance itself.

      $('input[type="checkbox"]').prop({
      checked: true,
      disabled: false,
      });
    • Set a property of an element.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • name: string

        Name of the property.

      • value:
            | string
            | boolean
            | ((this: Element, i: number, prop: string) => string | boolean)

        Value to set the property to.

      Returns Cheerio<T>

      The instance itself.

    • Get a property of an element.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • name: string

        The property's name.

      Returns string

      The property's value.

    • Method for removing attributes by name.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • name: string

        Name of the attribute.

      Returns Cheerio<T>

      The instance itself.

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

      $('.apple').attr('id', 'favorite');
      $('.apple').removeAttr('id class').prop('outerHTML');
      //=> <li>Apple</li>
    • 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

      Parameters

      • this: R
      • Optionalname: string | ((this: Element, i: number, className: string) => string)

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

      Returns R

      The instance itself.

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

      $('.apple').addClass('red').removeClass().prop('outerHTML');
      //=> <li class="">Apple</li>
    • 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

      Parameters

      • this: R
      • Optionalvalue:
            | string
            | (
                (
                    this: Element,
                    i: number,
                    className: string,
                    stateVal?: boolean,
                ) => string
            )

        Name of the class. Can also be a function.

      • OptionalstateVal: boolean

        If specified the state of the class.

      Returns R

      The instance itself.

      $('.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>
    • Method for getting the value of input, select, and textarea. Note: Support for map, and function has not been added yet.

      Type Parameters

      Parameters

      Returns string | string[]

      The value.

      $('input[type="text"]').val();
      //=> input_text
    • Method for setting the value of input, select, and textarea. Note: Support for map, and function has not been added yet.

      Type Parameters

      Parameters

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

        The new value.

      Returns Cheerio<T>

      The instance itself.

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

    CSS

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

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • Optionalnames: string[]

        Optionally the names of the properties 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.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • name: string

        The name of the property.

      Returns string

      The property value for the given name.

    • Set one CSS property for every matched element.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • prop: string

        The name of the property.

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

        The new value.

      Returns Cheerio<T>

      The instance itself.

    • Set multiple CSS properties for every matched element.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • map: Record<string, string>

        A map of property names and values.

      Returns Cheerio<T>

      The instance itself.

    Forms

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

      Type Parameters

      Parameters

      Returns string

      The serialized form.

      $('<form><input name="foo" value="bar" /></form>').serialize();
      //=> 'foo=bar'
    • Encode a set of form elements as an array of names and values.

      Type Parameters

      Parameters

      Returns { name: string; value: string }[]

      The serialized form.

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

    Manipulation

    append: <T extends AnyNode>(
        this: Cheerio<T>,
        ...elems:
            | [
                (
                    this: AnyNode,
                    i: number,
                    html: string,
                ) => BasicAcceptedElems<AnyNode>,
            ]
            | BasicAcceptedElems<AnyNode>[],
    ) => Cheerio<T>

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

    $('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>
    prepend: <T extends AnyNode>(
        this: Cheerio<T>,
        ...elems:
            | [
                (
                    this: AnyNode,
                    i: number,
                    html: string,
                ) => BasicAcceptedElems<AnyNode>,
            ]
            | BasicAcceptedElems<AnyNode>[],
    ) => Cheerio<T>

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

    $('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>
    wrap: <T extends AnyNode>(
        this: Cheerio<T>,
        wrapper: AcceptedElems<AnyNode>,
    ) => Cheerio<T>

    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 Declaration

    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>
    wrapInner: <T extends AnyNode>(
        this: Cheerio<T>,
        wrapper: AcceptedElems<AnyNode>,
    ) => 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.

    Type Declaration

    const redFruit = $('<div class="red-fruit"></div>');
    $('.apple').wrapInner(redFruit);

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

    const healthy = $('<div class="healthy"></div>');
    $('li').wrapInner(healthy);

    //=> <ul id="fruits">
    // <li class="apple">
    // <div class="healthy">Apple</div>
    // </li>
    // <li class="orange">
    // <div class="healthy">Orange</div>
    // </li>
    // <li class="pear">
    // <div class="healthy">Pear</div>
    // </li>
    // </ul>
    • Insert content next to each element in the set of matched elements.

      Type Parameters

      Parameters

      Returns Cheerio<T>

      The instance itself.

      $('.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>
    • Insert every element in the set of matched elements to the end of the target.

      Type Parameters

      Parameters

      Returns Cheerio<T>

      The instance itself.

      $('<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>
    • Insert content previous to each element in the set of matched elements.

      Type Parameters

      Parameters

      Returns Cheerio<T>

      The instance itself.

      $('.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>
    • Removes all children from each item in the selection. Text nodes and comment nodes are left as is.

      Type Parameters

      Parameters

      Returns Cheerio<T>

      The instance itself.

      $('ul').empty();
      $.html();
      //=> <ul id="fruits"></ul>
    • Gets an HTML content string from the first selected element.

      Type Parameters

      Parameters

      Returns string

      The HTML content string.

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

      $('#fruits').html('<li class="mango">Mango</li>').html();
      //=> <li class="mango">Mango</li>
    • Replaces each selected element's content with the specified content.

      Type Parameters

      Parameters

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

        The content to replace selection's contents with.

      Returns Cheerio<T>

      The instance itself.

      $('.orange').html('<li class="mango">Mango</li>').html();
      //=> <li class="mango">Mango</li>
    • Insert every element in the set of matched elements after the target.

      Type Parameters

      Parameters

      Returns Cheerio<T>

      The set of newly inserted elements.

      $('<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>
    • Insert every element in the set of matched elements before the target.

      Type Parameters

      Parameters

      Returns Cheerio<T>

      The set of newly inserted elements.

      $('<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>
    • Insert every element in the set of matched elements to the beginning of the target.

      Type Parameters

      Parameters

      Returns Cheerio<T>

      The instance itself.

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

      Parameters

      • this: Cheerio<T>
      • Optionalselector: string

        Optional selector for elements to remove.

      Returns Cheerio<T>

      The instance itself.

      $('.pear').remove();
      $.html();
      //=> <ul id="fruits">
      // <li class="apple">Apple</li>
      // <li class="orange">Orange</li>
      // </ul>
    • Replaces matched elements with content.

      Type Parameters

      Parameters

      Returns Cheerio<T>

      The instance itself.

      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>
    • Get the combined text contents of each element in the set of matched elements, including their descendants.

      Type Parameters

      Parameters

      Returns string

      The text contents of the collection.

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

      $('ul').text();
      //=> Apple
      // Orange
      // Pear
    • Set the content of each element in the set of matched elements to the specified 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.

      $('.orange').text('Orange');
      //=> <div class="orange">Orange</div>
    • Turns the collection to a string. Alias for .html().

      Type Parameters

      Parameters

      Returns string

      The rendered document.

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

      Type Parameters

      Parameters

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

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

      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.

      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>

    Other

    cheerio: "[cheerio object]"
    length: number
    prevObject: Cheerio<any>
    splice: {
        (start: number, deleteCount?: number): any[];
        (start: number, deleteCount: number, ...items: any[]): any[];
    }

    Type Declaration

      • (start: number, deleteCount?: number): 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.

        • OptionaldeleteCount: number

          The number of elements to remove. Omitting this argument will remove all elements from the start paramater location to end of the array. If value of this argument is either a negative number, zero, undefined, or a type that cannot be converted to an integer, the function will evaluate the argument as zero and not remove any elements.

        Returns any[]

        An array containing the elements that were deleted.

      • (start: number, deleteCount: number, ...items: any[]): 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. If value of this argument is either a negative number, zero, undefined, or a type that cannot be converted to an integer, the function will evaluate the argument as zero and not remove any elements.

        • ...items: any[]

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

        Returns any[]

        An array containing the elements that were deleted.

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

      Type Parameters

      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.

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

      Type Parameters

      • T

      Parameters

      Returns T[]

      The contained items.

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

    Traversing

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

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

    Type Declaration

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

    $('#fruits').children('.pear').text();
    //=> Pear
    next: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

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

    Type Declaration

    $('.apple').next().hasClass('orange');
    //=> true
    nextAll: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

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

    Type Declaration

    $('.apple').nextAll();
    //=> [<li class="orange">Orange</li>, <li class="pear">Pear</li>]
    $('.apple').nextAll('.orange');
    //=> [<li class="orange">Orange</li>]
    nextUntil: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element> | null,
        filterSelector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

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

    Type Declaration

    $('.apple').nextUntil('.pear');
    //=> [<li class="orange">Orange</li>]
    parent: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

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

    Type Declaration

    $('.pear').parent().attr('id');
    //=> fruits
    parents: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

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

    Type Declaration

    $('.orange').parents().length;
    //=> 2
    $('.orange').parents('#fruits').length;
    //=> 1
    parentsUntil: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element> | null,
        filterSelector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

    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 Declaration

    $('.orange').parentsUntil('#food').length;
    //=> 1
    prev: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

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

    Type Declaration

    $('.orange').prev().hasClass('apple');
    //=> true
    prevAll: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

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

    Type Declaration

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

    $('.pear').prevAll('.orange');
    //=> [<li class="orange">Orange</li>]
    prevUntil: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element> | null,
        filterSelector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

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

    Type Declaration

    $('.pear').prevUntil('.apple');
    //=> [<li class="orange">Orange</li>]
    siblings: <T extends AnyNode>(
        this: Cheerio<T>,
        selector?: AcceptedFilters<Element>,
    ) => Cheerio<Element>

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

    Type Declaration

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

    $('.pear').siblings('.orange').length;
    //=> 1
    • Add the previous set of elements on the stack to the current set, optionally filtered by a selector.

      Type Parameters

      Parameters

      • this: Cheerio<T>
      • Optionalselector: string

        Selector for the elements to add.

      Returns Cheerio<AnyNode>

      The combined set.

      $('li').eq(0).addBack('.orange').length;
      //=> 2
    • 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

      Parameters

      Returns Cheerio<AnyNode>

      The closest nodes.

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

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

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

      $('.orange').closest('#fruits');
      //=> [<ul id="fruits"> ... </ul>]
    • 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

      Parameters

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

        Function to execute.

      Returns Cheerio<T>

      The instance itself, useful for chaining.

      const fruits = [];

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

      fruits.join(', ');
      //=> Apple, Orange, Pear
    • End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.

      Type Parameters

      • T

      Parameters

      Returns Cheerio<AnyNode>

      The previous state of the set of matched elements.

      $('li').eq(0).end().length;
      //=> 3
    • 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

      Parameters

      • this: Cheerio<T>
      • i: number

        Index of the element to select.

      Returns Cheerio<T>

      The element at the ith position.

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

      $('li').eq(-1).text();
      //=> Pear
    • 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
      • S

      Parameters

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

        Value to look for, following the rules above.

      Returns Cheerio<S>

      The filtered collection.

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

      Parameters

      Returns Cheerio<S extends string ? Element : T>

      The filtered collection.

      $('li').filter('.orange').attr('class');
      //=> orange
      $('li')
      .filter(function (i, el) {
      // this === el
      return $(this).attr('class') === 'orange';
      })
      .attr('class'); //=> orange
    • Retrieve one of the elements matched by the Cheerio object, at the ith position.

      Type Parameters

      • T

      Parameters

      • this: Cheerio<T>
      • i: number

        Element to retrieve.

      Returns T

      The element at the ith position.

      $('li').get(0).tagName;
      //=> li
    • Retrieve all elements matched by the Cheerio object, as an array.

      Type Parameters

      • T

      Parameters

      Returns T[]

      All elements matched by the Cheerio object.

      $('li').get().length;
      //=> 3
    • 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

      Returns Cheerio<AnyNode>

      The filtered collection.

      $('ul').has('.pear').attr('id');
      //=> fruits
      $('ul').has($('.pear')[0]).attr('id');
      //=> fruits
    • Search for a given element from among the matched elements.

      Type Parameters

      Parameters

      Returns number

      The index of the element.

      $('.pear').index();
      //=> 2 $('.orange').index('li');
      //=> 1
      $('.apple').index($('#fruit, li'));
      //=> 1
    • 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

      Parameters

      Returns boolean

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

    • Will select the last element of a cheerio object.

      Type Parameters

      • T

      Parameters

      Returns Cheerio<T>

      The last element.

      $('#fruits').children().last().text();
      //=> Pear
    • 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
      • M

      Parameters

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

        Function to execute.

      Returns Cheerio<M>

      The mapped elements, wrapped in a Cheerio collection.

      $('li')
      .map(function (i, el) {
      // this === el
      return $(this).text();
      })
      .toArray()
      .join(' ');
      //=> "apple orange pear"
    • 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

      Parameters

      Returns Cheerio<T>

      The filtered collection.

      $('li').not('.apple').length;
      //=> 2
      $('li').not(function (i, el) {
      // this === el
      return $(this).attr('class') === 'orange';
      }).length; //=> 2
    • Gets the elements matching the specified range (0-based position).

      Type Parameters

      • T

      Parameters

      • this: Cheerio<T>
      • Optionalstart: number

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

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

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

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