Class: Cheerio<T>
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')
).
Example
<ul id="fruits">
<li class="apple">Apple</li>
<li class="orange">Orange</li>
<li class="pear">Pear</li>
</ul>
Type parameters
Name |
---|
T |
Hierarchy
-
MethodsType
-
Iterable
<T
>↳
Cheerio
Implements
ArrayLike
<T
>
Indexable
▪ [index: number
]: T
Attributes
addClass
▸ addClass<T
, R
>(this
, value?
): R
Adds class(es) to all of the matched elements. Also accepts a function
.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
R | extends ArrayLike <T > |
Parameters
Name | Type | Description |
---|---|---|
this | R | - |
value? | string | (this : Element , i : number , className : string ) => undefined | string | Name of new class. |
Returns
R
The instance itself.
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/
Defined in
attr
▸ attr<T
>(this
, name
): string
| undefined
Method for getting attributes. Gets the attribute value for only the first element in the matched set.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | string | Name of the attribute. |
Returns
string
| undefined
The attribute's value.
Example
$('ul').attr('id');
//=> fruits
See
Defined in
▸ attr<T
>(this
): Record
<string
, string
> | undefined
Method for getting all attributes and their values of the first element in the matched set.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type |
---|---|
this | Cheerio <T > |
Returns
Record
<string
, string
> | undefined
The attribute's values.
Example
$('ul').attr();
//=> { id: 'fruits' }
See
Defined in
▸ attr<T
>(this
, name
, value?
): Cheerio
<T
>
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
.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | string | Name of the attribute. |
value? | null | string | (this : Element , i : number , attrib : string ) => null | string | The new value of the attribute. |
Returns
Cheerio
<T
>
The instance itself.
Example
$('.apple').attr('id', 'favorite').html();
//=> <li class="apple" id="favorite">Apple</li>
See
Defined in
▸ attr<T
>(this
, values
): Cheerio
<T
>
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.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
values | Record <string , null | string > | Map of attribute names and values. |
Returns
Cheerio
<T
>
The instance itself.
Example
$('.apple').attr({ id: 'favorite' }).html();
//=> <li class="apple" id="favorite">Apple</li>
See
Defined in
data
▸ data<T
>(this
, name
): unknown
| undefined
Method for getting data attributes, for only the first element in the matched set.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
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.
Example
$('<div data-apple-color="red"></div>').data('apple-color');
//=> 'red'
See
Defined in
▸ data<T
>(this
): Record
<string
, unknown
>
Method for getting all of an element's data attributes, for only the first element in the matched set.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type |
---|---|
this | Cheerio <T > |
Returns
Record
<string
, unknown
>
A map with all of the data attributes.
Example
$('<div data-apple-color="red"></div>').data();
//=> { appleColor: 'red' }
See
Defined in
▸ data<T
>(this
, name
, value
): Cheerio
<T
>
Method for setting data attributes, for only the first element in the matched set.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | string | Name of the data attribute. |
value | unknown | The new value. |
Returns
Cheerio
<T
>
The instance itself.
Example
const apple = $('.apple').data('kind', 'mac');
apple.data('kind');
//=> 'mac'
See
Defined in
▸ data<T
>(this
, values
): Cheerio
<T
>
Method for setting multiple data attributes at once, for only the first element in the matched set.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
values | Record <string , unknown > | Map of names to values. |
Returns
Cheerio
<T
>
The instance itself.
Example
const apple = $('.apple').data({ kind: 'mac' });
apple.data('kind');
//=> 'mac'
See
Defined in
hasClass
▸ hasClass<T
>(this
, className
): boolean
Check to see if any of the matched elements have the given className
.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
className | string | Name of the class. |
Returns
boolean
Indicates if an element has the given className
.
Example
$('.pear').hasClass('pear');
//=> true
$('apple').hasClass('fruit');
//=> false
$('li').hasClass('pear');
//=> true
See
https://api.jquery.com/hasClass/
Defined in
prop
▸ prop<T
>(this
, name
): string
| undefined
Method for getting and setting properties. Gets the property value for only the first element in the matched set.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | "tagName" | "nodeName" | Name of the property. |
Returns
string
| undefined
If value
is specified the instance itself, otherwise the prop's
value.
Example
$('input[type="checkbox"]').prop('checked');
//=> false
$('input[type="checkbox"]').prop('checked', true).val();
//=> ok
See
Defined in
▸ prop<T
>(this
, name
): string
| null
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type |
---|---|
this | Cheerio <T > |
name | "innerText" | "outerHTML" | "textContent" | "innerHTML" |
Returns
string
| null
Defined in
▸ prop<T
>(this
, name
): StyleProp
| undefined
Get a parsed CSS style object.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | "style" | Name of the property. |
Returns
StyleProp
| undefined
The style object, or undefined
if the element has no style
attribute.
Defined in
▸ prop<T
>(this
, name
): string
| undefined
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
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | "href" | "src" | Name of the property. |
Returns
string
| undefined
The resolved URL, or undefined
if the element is not supported.
Example
$('<img src="image.png">').prop('src');
//=> 'https://example.com/image.png'
Defined in
▸ prop<T
, K
>(this
, name
): Element
[K
]
Get a property of an element.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
K | extends keyof Element |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | K | Name of the property. |
Returns
Element
[K
]
The property's value.
Defined in
▸ prop<T
, K
>(this
, name
, value
): Cheerio
<T
>
Set a property of an element.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
K | extends keyof Element |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | K | Name of the property. |
value | Element [K ] | (this : Element , i : number , prop : K ) => undefined | null | string | number | Record <string , string > | TagSourceCodeLocation | Document | Element | CDATA | Text | Comment | ProcessingInstruction | ChildNode [] | { [name: string] : string ; } | Attribute [] | <T>(this : T , recursive? : boolean ) => T | Value to set the property to. |
Returns
Cheerio
<T
>
The instance itself.
Defined in
▸ prop<T
>(this
, map
): Cheerio
<T
>
Set multiple properties of an element.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
map | Record <string , undefined | null | string | number | boolean | Record <string , string > | TagSourceCodeLocation | Document | Element | CDATA | Text | Comment | ProcessingInstruction | ChildNode [] | { [name: string] : string ; } | Attribute [] | <T>(this : T , recursive? : boolean ) => T > | Object of properties to set. |
Returns
Cheerio
<T
>
The instance itself.
Example
$('input[type="checkbox"]').prop({
checked: true,
disabled: false,
});
Defined in
▸ prop<T
>(this
, name
, value
): Cheerio
<T
>
Set a property of an element.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | string | Name of the property. |
value | null | string | boolean | (this : Element , i : number , prop : string ) => string | boolean | Value to set the property to. |
Returns
Cheerio
<T
>
The instance itself.
Defined in
▸ prop<T
>(this
, name
): string
Get a property of an element.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | string | The property's name. |
Returns
string
The property's value.
Defined in
removeAttr
▸ removeAttr<T
>(this
, name
): Cheerio
<T
>
Method for removing attributes by name
.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | string | Name of the attribute. |
Returns
Cheerio
<T
>
The instance itself.
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/
Defined in
removeClass
▸ removeClass<T
, R
>(this
, name?
): 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
.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
R | extends ArrayLike <T > |
Parameters
Name | Type | Description |
---|---|---|
this | R | - |
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.
Example
$('.pear').removeClass('pear').html();
//=> <li class="">Pear</li>
$('.apple').addClass('red').removeClass().html();
//=> <li class="">Apple</li>
See
https://api.jquery.com/removeClass/
Defined in
toggleClass
▸ toggleClass<T
, R
>(this
, value?
, stateVal?
): 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
.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
R | extends ArrayLike <T > |
Parameters
Name | Type | Description |
---|---|---|
this | R | - |
value? | string | (this : Element , i : number , className : string , stateVal? : boolean ) => string | Name of the class. Can also be a function. |
stateVal? | boolean | If specified the state of the class. |
Returns
R
The instance itself.
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/
Defined in
val
▸ val<T
>(this
): string
| undefined
| string
[]
Method for getting the value of input, select, and textarea. Note: Support
for map
, and function
has not been added yet.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type |
---|---|
this | Cheerio <T > |
Returns
string
| undefined
| string
[]
The value.
Example
$('input[type="text"]').val();
//=> input_text
See
Defined in
▸ val<T
>(this
, value
): Cheerio
<T
>
Method for setting the value of input, select, and textarea. Note: Support
for map
, and function
has not been added yet.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
value | string | string [] | The new value. |
Returns
Cheerio
<T
>
The instance itself.
Example
$('input[type="text"]').val('test').html();
//=> <input type="text" value="test"/>
See
Defined in
CSS
css
▸ css<T
>(this
, names?
): Record
<string
, string
> | undefined
Get the value of a style property for the first element in the set of matched elements.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
names? | string [] | Optionally the names of the properties of interest. |
Returns
Record
<string
, string
> | undefined
A map of all of the style properties.
See
Defined in
▸ css<T
>(this
, name
): string
| undefined
Get the value of a style property for the first element in the set of matched elements.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
name | string | The name of the property. |
Returns
string
| undefined
The property value for the given name.
See
Defined in
▸ css<T
>(this
, prop
, val
): Cheerio
<T
>
Set one CSS property for every matched element.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
prop | string | The name of the property. |
val | string | (this : Element , i : number , style : string ) => undefined | string | The new value. |
Returns
Cheerio
<T
>
The instance itself.
See
Defined in
▸ css<T
>(this
, map
): Cheerio
<T
>
Set multiple CSS properties for every matched element.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type | Description |
---|---|---|
this | Cheerio <T > | - |
map | Record <string , string > | A map of property names and values. |
Returns
Cheerio
<T
>
The instance itself.
See
Defined in
Forms
serialize
▸ serialize<T
>(this
): string
Encode a set of form elements as a string for submission.
Type parameters
Name | Type |
---|---|
T | extends AnyNode |
Parameters
Name | Type |
---|---|
this | Cheerio <T > |