cheerio/attributes

Methods for getting and modifying attributes.

Methods

# (static) 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>

# (static) 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>

# (static) 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'

# (static) 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

# (static) 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

# (static) 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

# (static) 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>

# (static) 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>

# (static) 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>

# (static) 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"/>

# (inner) splitNames(names) → {Array.<string>}

Splits a space-separated list of names to individual names.

Parameters:
Name Type Description
names string

Names to split.

Returns:
  • Split names.
Type
Array.<string>