Methods for getting and modifying attributes.
- Source:
Methods
# (static) addClass(value) → {Cheerio}
Adds class(es) to all of the matched elements. Also accepts a function
like jQuery.
Name | Type | Description |
---|---|---|
value |
string | function | Name of new class. |
- Source:
The instance itself.
- Type
- Cheerio
$('.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.
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | Name of the attribute. |
|
value |
string | function |
<optional> |
If specified sets the value of the attribute. |
- Source:
If value
is specified the instance itself,
otherwise the attribute's value.
- Type
- string | Cheerio
$('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.
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | Name of the attribute. |
|
value |
any |
<optional> |
If specified new value. |
- Source:
If value
is specified the instance itself,
otherwise the data attribute's value.
- Type
- string | Cheerio | undefined
$('<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
.
Name | Type | Description |
---|---|---|
className |
string | Name of the class. |
- Source:
Indicates if an element has the given className
.
- Type
- boolean
$('.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.
Name | Type | Description |
---|---|---|
selector |
string | function | Cheerio | Node | Selector for the selection. |
- Source:
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.
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | Name of the property. |
|
value |
any |
<optional> |
If specified set the property to this. |
- Source:
If value
is specified the instance itself,
otherwise the prop's value.
- Type
- string | Cheerio
$('input[type="checkbox"]').prop('checked');
//=> false
$('input[type="checkbox"]').prop('checked', true).val();
//=> ok
# (static) removeAttr(name) → {Cheerio}
Method for removing attributes by name
.
Name | Type | Description |
---|---|---|
name |
string | Name of the attribute. |
- Source:
The instance itself.
- Type
- Cheerio
$('.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.
Name | Type | Description |
---|---|---|
value |
string | function | Name of the class. |
- Source:
The instance itself.
- Type
- Cheerio
$('.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.
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. |
- Source:
The instance itself.
- Type
- Cheerio
$('.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.
Name | Type | Attributes | Description |
---|---|---|---|
value |
string | Array.<string> |
<optional> |
If specified new value. |
- Source:
If a new value
is specified the instance
itself, otherwise the value.
- Type
- string | Cheerio | undefined
$('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.
Name | Type | Description |
---|---|---|
names |
string | Names to split. |
- Source:
- Split names.
- Type
- Array.<string>