Class: abstract
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>
Extends
MethodsType
.Iterable
<T
>
Type Parameters
• T
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
• T extends AnyNode
• R extends ArrayLike
<T
>
Parameters
• this: R
• value?: string
| (this
, i
, className
) => 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(this, name)
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
• T extends AnyNode
Parameters
• 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(this)
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
• T extends AnyNode
Parameters
• this: Cheerio
<T
>
Returns
Record
<string
, string
> | undefined
The attribute's values.
Example
$('ul').attr();
//=> { id: 'fruits' }
See
Defined in
attr(this, name, value)
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
• T extends AnyNode
Parameters
• this: Cheerio
<T
>
• name: string
Name of the attribute.
• value?: null
| string
| (this
, i
, attrib
) => 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(this, values)
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
• T extends AnyNode
Parameters
• 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(this, name)
data<
T
>(this
,name
):unknown
|undefined
Method for getting data attributes, for only the first element in the matched set.
Type Parameters
• T extends AnyNode
Parameters
• 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(this)
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
• T extends AnyNode
Parameters
• 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(this, name, value)
data<
T
>(this
,name
,value
):Cheerio
<T
>
Method for setting data attributes, for only the first element in the matched set.
Type Parameters
• T extends AnyNode
Parameters
• 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(this, values)
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
• T extends AnyNode
Parameters
• 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
• T extends AnyNode
Parameters
• 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(this, name)
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
• T extends AnyNode
Parameters
• 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(this, name)
prop<
T
>(this
,name
):string
|null
Type Parameters
• T extends AnyNode
Parameters
• this: Cheerio
<T
>
• name: "innerText"
| "outerHTML"
| "textContent"
| "innerHTML"
Returns
string
| null
Defined in
prop(this, name)
prop<
T
>(this
,name
):StyleProp
|undefined
Get a parsed CSS style object.
Type Parameters
• T extends AnyNode
Parameters
• 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(this, name)
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
• T extends AnyNode
Parameters
• 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(this, name)
prop<
T
,K
>(this
,name
):Element
[K
]
Get a property of an element.
Type Parameters
• T extends AnyNode
• K extends keyof Element
Parameters
• this: Cheerio
<T
>
• name: K
Name of the property.
Returns
Element
[K
]
The property's value.
Defined in
prop(this, name, value)
prop<
T
,K
>(this
,name
,value
):Cheerio
<T
>
Set a property of an element.
Type Parameters
• T extends AnyNode
• K extends keyof Element
Parameters
• this: Cheerio
<T
>
• name: K
Name of the property.
• value: Element
[K
] | (this
, i
, prop
) => undefined
| null
| string
| number
| Record
<string
, string
> | TagSourceCodeLocation
| Document
| Element
| CDATA
| Text
| Comment
| ProcessingInstruction
| ChildNode
[] | object
| Attribute
[] | <T
>(this
, recursive
?) => T
Value to set the property to.
Returns
Cheerio
<T
>
The instance itself.
Defined in
prop(this, map)
prop<
T
>(this
,map
):Cheerio
<T
>
Set multiple properties of an element.
Type Parameters
• T extends AnyNode