Cheerio

Cheerio

# new Cheerio(selector, contextopt, rootopt, optionsopt)

Instance of cheerio. Methods are specified in the modules. Usage of this constructor is not recommended. Please use $.load instead.

Parameters:
Name Type Attributes Description
selector string | Cheerio | Node | Array.<Node>

The new selection.

context string | Cheerio | Node | Array.<Node> <optional>

Context of the selection.

root string | Cheerio | Node | Array.<Node> <optional>

Sets the root node.

options object <optional>

Options for the instance.

Members

# cheerio

Set a signature of the object.

Methods

# (static) contains(container, contained) → {boolean}

Checks to see if the contained DOM element is a descendant of the container DOM element.

Parameters:
Name Type Description
container Node

Potential parent node.

contained Node

Potential child node.

Returns:

Indicates if the nodes contain one another.

Type
boolean

# (static) merge(arr1, arr2) → {Array|Cheerio}

$.merge().

Parameters:
Name Type Description
arr1 Array | Cheerio

First array.

arr2 Array | Cheerio

Second array.

Returns:

arr1, with elements of arr2 inserted.

Type
Array | Cheerio

# (static) parseHTML(data, contextopt, keepScriptsopt) → {Array.<Node>}

Parses a string into an array of DOM nodes. The context argument has no meaning for Cheerio, but it is maintained for API compatibility with jQuery.

Parameters:
Name Type Attributes Description
data string

Markup that will be parsed.

context any | boolean <optional>

Will be ignored. If it is a boolean it will be used as the value of keepScripts.

keepScripts boolean <optional>

If false all scripts will be removed.

Returns:

The parsed DOM.

Type
Array.<Node>

# (static) root() → {Cheerio}

Sometimes you need to work with the top-level root element. To query it, you can use $.root().

Returns:

Cheerio instance wrapping the root node.

Type
Cheerio
Example
$.root().append('<ul id="vegetables"></ul>').html();
  //=> <ul id="fruits">...</ul><ul id="vegetables"></ul>

# add(other, contextopt) → {Cheerio}

Add elements to the set of matched elements.

Parameters:
Name Type Attributes Description
other string | Cheerio

Elements to add.

context Cheerio <optional>

Optionally the context of the new selection.

Returns:

The combined set.

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

# addBack(selector) → {Cheerio}

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

Parameters:
Name Type Description
selector string

Selector for the elements to add.

Returns:

The combined set.

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

# addClass(value) → {Cheerio}

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

Parameters:
Name Type Description
value string | function

Name of new class.

Returns:

The instance itself.

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

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

# after() → {Cheerio}

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

Returns:

The instance itself.

Type
Cheerio
Example
$('.apple').after('<li class="plum">Plum</li>');
  $.html();
  //=>  <ul id="fruits">
  //      <li class="apple">Apple</li>
  //      <li class="plum">Plum</li>
  //      <li class="orange">Orange</li>
  //      <li class="pear">Pear</li>
  //    </ul>

# append()

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

Example
$('ul').append('<li class="plum">Plum</li>');
  $.html();
  //=>  <ul id="fruits">
  //      <li class="apple">Apple</li>
  //      <li class="orange">Orange</li>
  //      <li class="pear">Pear</li>
  //      <li class="plum">Plum</li>
  //    </ul>

# appendTo(target) → {Cheerio}

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

Parameters:
Name Type Description
target string | Cheerio

Element to append elements to.

Returns:

The instance itself.

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

# attr(name, valueopt) → {string|Cheerio}

Method for getting and setting attributes. Gets 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 like jQuery.

Parameters:
Name Type Attributes Description
name string

Name of the attribute.

value string | function <optional>

If specified sets the value of the attribute.

Returns:

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

Type
string | Cheerio
Example
$('ul').attr('id');
  //=> fruits

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

# before() → {Cheerio}

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

Returns:

The instance itself.

Type
Cheerio
Example
$('.apple').before('<li class="plum">Plum</li>');
  $.html();
  //=>  <ul id="fruits">
  //      <li class="plum">Plum</li>
  //      <li class="apple">Apple</li>
  //      <li class="orange">Orange</li>
  //      <li class="pear">Pear</li>
  //    </ul>

# children(selectoropt) → {Cheerio}

Gets the children of the first selected element.

Parameters:
Name Type Attributes Description
selector string <optional>

If specified filter for children.

Returns:

The children.

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

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

# clone() → {Cheerio}

Clone the cheerio object.

Returns:

The cloned object.

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

# closest(selectoropt) → {Cheerio}

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.

Parameters:
Name Type Attributes Description
selector string <optional>

Selector for the element to find.

Returns:

The closest nodes.

Type
Cheerio
Example
$('.orange').closest();
  // => []
  $('.orange').closest('.apple');
  // => []
  $('.orange').closest('li');
  // => [<li class="orange">Orange</li>]
  $('.orange').closest('#fruits');
  // => [<ul id="fruits"> ... </ul>]

# contents() → {Cheerio}

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

Returns:

The children.

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

# css(prop, valopt) → {Cheerio}

Get the value of a style property for the first element in the set of matched elements or set one or more CSS properties for every matched element.

Parameters:
Name Type Attributes Description
prop string | object

The name of the property.

val string <optional>

If specified the new value.

Returns:

The instance itself.

Type
Cheerio

# data(name, valueopt) → {string|Cheerio|undefined}

Method for getting and setting data attributes. Gets or sets the data attribute value for only the first element in the matched set.

Parameters:
Name Type Attributes Description
name string

Name of the attribute.

value any <optional>

If specified new value.

Returns:

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

Type
string | Cheerio | undefined
Example
$('<div data-apple-color="red"></div>').data();
  //=> { appleColor: 'red' }

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

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

# each(fn) → {Cheerio}

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.

Parameters:
Name Type Description
fn function

Function to execute.

Returns:

The instance itself, useful for chaining.

Type
Cheerio
Example
const fruits = [];

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

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

# empty() → {Cheerio}

Empties an element, removing all its children.

Returns:

The instance itself.

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

# end() → {Cheerio}

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

Returns:

The previous state of the set of matched elements.

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

# eq(i) → {Cheerio}

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

Parameters:
Name Type Description
i number

Index of the element to select.

Returns:

The element at the ith position.

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

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

# filter(match, containeropt) → {Cheerio}

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.

Parameters:
Name Type Attributes Description
match string | function

Value to look for, following the rules above.

container Cheerio <optional>

Optional node to filter instead.

Returns:

The filtered collection.

Type
Cheerio
Examples

Selector

  $('li').filter('.orange').attr('class');
  //=> orange

Function

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

# find(selectorOrHaystack) → {Cheerio}

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

Parameters:
Name Type Description
selectorOrHaystack string | Cheerio | Node

Element to look for.

Returns:

The found elements.

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

# first() → {Cheerio}

Will select the first element of a cheerio object.

Returns:

The first element.

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

# get(iopt) → {Node}

Retrieve the DOM elements matched by the Cheerio object. If an index is specified, retrieve one of the elements matched by the Cheerio object.

Parameters:
Name Type Attributes Description
i number <optional>

Element to retrieve.

Returns:

The node at the ith position.

Type
Node
Examples
$('li').get(0).tagName
  //=> li

  If no index is specified, retrieve all elements matched by the Cheerio object:
$('li').get().length;
  //=> 3

# has(selectorOrHaystack) → {Cheerio}

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:
Name Type Description
selectorOrHaystack string | Cheerio | Node

Element to look for.

Returns:

The filtered collection.

Type
Cheerio
Examples

Selector

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

Element

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

# hasClass(className) → {boolean}

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

Parameters:
Name Type Description
className string

Name of the class.

Returns:

Indicates if an element has the given className.

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

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

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

# html(str) → {Cheerio}

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.

Parameters:
Name Type Description
str string | Cheerio

If specified used to replace selection's contents.

Returns:

The instance itself.

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

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

# index(selectorOrNeedleopt) → {number}

Search for a given element from among the matched elements.

Parameters:
Name Type Attributes Description
selectorOrNeedle string | Cheerio | Node <optional>

Element to look for.

Returns:

The index of the element.

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

# insertAfter(target) → {Cheerio}

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

Parameters:
Name Type Description
target string | Cheerio

Element to insert elements after.

Returns:

The set of newly inserted elements.

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

# insertBefore(target) → {Cheerio}

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

Parameters:
Name Type Description
target string | Cheerio

Element to insert elements before.

Returns:

The set of newly inserted elements.

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

# is(selector) → {boolean}

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.

Parameters:
Name Type Description
selector string | function | Cheerio | Node

Selector for the selection.

Returns:

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

Type
boolean

# last() → {Cheerio}

Will select the last element of a cheerio object.

Returns:

The last element.

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

# map(fn) → {Cheerio}

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.

Parameters:
Name Type Description
fn function

Function to execute.

Returns:

The mapped elements, wrapped in a Cheerio collection.

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

# next(selectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string <optional>

If specified filter for sibling.

Returns:

The next nodes.

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

# nextAll(selectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string <optional>

If specified filter for siblings.

Returns:

The next nodes.

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

# nextUntil(selector, filterSelectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string | Cheerio | Node

Selector for element to stop at.

filterSelector string <optional>

If specified filter for siblings.

Returns:

The next nodes.

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

# not(match, containeropt) → {Cheerio}

Remove elements from the set of matched elements. Given a jQuery object that represents a set of DOM elements, the .not() method constructs a new jQuery 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.

Parameters:
Name Type Attributes Description
match string | function

Value to look for, following the rules above.

container Array.<Node> | Cheerio <optional>

Optional node to filter instead.

Returns:

The filtered collection.

Type
Cheerio
Examples

Selector

  $('li').not('.apple').length;
  //=> 2

Function

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

# parent(selectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string <optional>

If specified filter for parent.

Returns:

The parents.

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

# parents(selectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string <optional>

If specified filter for parents.

Returns:

The parents.

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

# parentsUntil(selector, filteropt) → {Cheerio}

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.

Parameters:
Name Type Attributes Description
selector string | Node | Cheerio

Selector for element to stop at.

filter string | function <optional>

Optional filter for parents.

Returns:

The parents.

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

# prepend()

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

Example
$('ul').prepend('<li class="plum">Plum</li>');
  $.html();
  //=>  <ul id="fruits">
  //      <li class="plum">Plum</li>
  //      <li class="apple">Apple</li>
  //      <li class="orange">Orange</li>
  //      <li class="pear">Pear</li>
  //    </ul>

# prependTo(target) → {Cheerio}

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

Parameters:
Name Type Description
target string | Cheerio

Element to prepend elements to.

Returns:

The instance itself.

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

# prev(selectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string <optional>

If specified filter for siblings.

Returns:

The previous nodes.

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

# prevAll(selectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string <optional>

If specified filter for siblings.

Returns:

The previous nodes.

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

# prevUntil(selector, filterSelectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string | Cheerio | Node

Selector for element to stop at.

filterSelector string <optional>

If specified filter for siblings.

Returns:

The previous nodes.

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

# prop(name, valueopt) → {string|Cheerio}

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

Parameters:
Name Type Attributes Description
name string

Name of the property.

value any <optional>

If specified set the property to this.

Returns:

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

Type
string | Cheerio
Example
$('input[type="checkbox"]').prop('checked');
  //=> false

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

# remove(selectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string <optional>

Optional selector for elements to remove.

Returns:

The instance itself.

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

# removeAttr(name) → {Cheerio}

Method for removing attributes by name.

Parameters:
Name Type Description
name string

Name of the attribute.

Returns:

The instance itself.

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

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

# removeClass(value) → {Cheerio}

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 like jQuery.

Parameters:
Name Type Description
value string | function

Name of the class.

Returns:

The instance itself.

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

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

# replaceWith(content) → {Cheerio}

Replaces matched elements with content.

Parameters:
Name Type Description
content Cheerio | function

Replacement for matched elements.

Returns:

The instance itself.

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

# serialize() → {string}

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

Returns:

The serialized form.

Type
string

# serializeArray() → {Array.<object>}

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

This:
Returns:

The serialized form.

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

# siblings(selectoropt) → {Cheerio}

Gets the first selected element's siblings, excluding itself.

Parameters:
Name Type Attributes Description
selector string <optional>

If specified filter for siblings.

Returns:

The siblings.

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

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

# slice() → {Cheerio}

Gets the elements matching the specified range.

Returns:

The elements matching the specified range.

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

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

# text(stropt) → {Cheerio|string}

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.

Parameters:
Name Type Attributes Description
str string | function <optional>

If specified replacement for the selected element's contents.

Returns:

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

Type
Cheerio | string
Example
$('.orange').text();
  //=> Orange

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

# toArray() → {Array.<Node>}

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

Returns:

The contained items.

Type
Array.<Node>
Example
$('li').toArray(); //=> [ {...}, {...}, {...} ]

# toggleClass(value, stateValopt) → {Cheerio}

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 like jQuery.

Parameters:
Name Type Attributes Description
value string | function

Name of the class. Can also be a function.

stateVal boolean <optional>

If specified the state of the class.

Returns:

The instance itself.

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

# toString() → {string}

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

Returns:

The rendered document.

Type
string

# unwrap(selectoropt) → {Cheerio}

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

Parameters:
Name Type Attributes Description
selector string <optional>

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:

The instance itself, for chaining.

Type
Cheerio
Examples

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>

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>

# val(valueopt) → {string|Cheerio|undefined}

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

Parameters:
Name Type Attributes Description
value string | Array.<string> <optional>

If specified new value.

Returns:

If a new value is specified the instance itself, otherwise the value.

Type
string | Cheerio | undefined
Example
$('input[type="text"]').val();
  //=> input_text

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

# wrap(wrapper)

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.

Parameters:
Name Type Description
wrapper Cheerio

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

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>

# wrapAll(wrapper) → {Cheerio}

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.

Parameters:
Name Type Description
wrapper Cheerio

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

Returns:

The instance itself.

Type
Cheerio
Examples

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>

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>

# wrapInner(wrapper)

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.

Parameters:
Name Type Description
wrapper Cheerio

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

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>