/** * Methods for getting and modifying attributes. * * @module cheerio/attributes */ import type { AnyNode, Element } from 'domhandler'; import type { Cheerio } from '../cheerio.js'; /** * Method for getting attributes. Gets the attribute value for only the first * element in the matched set. * * @category Attributes * @example * * ```js * $('ul').attr('id'); * //=> fruits * ``` * * @param name - Name of the attribute. * @returns The attribute's value. * @see {@link https://api.jquery.com/attr/} */ export declare function attr(this: Cheerio, name: string): string | undefined; /** * Method for getting all attributes and their values of the first element in * the matched set. * * @category Attributes * @example * * ```js * $('ul').attr(); * //=> { id: 'fruits' } * ``` * * @returns The attribute's values. * @see {@link https://api.jquery.com/attr/} */ export declare function attr(this: Cheerio): Record | undefined; /** * 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`. * * @category Attributes * @example * * ```js * $('.apple').attr('id', 'favorite').html(); * //=>
  • Apple
  • * ``` * * @param name - Name of the attribute. * @param value - The new value of the attribute. * @returns The instance itself. * @see {@link https://api.jquery.com/attr/} */ export declare function attr(this: Cheerio, name: string, value?: string | null | ((this: Element, i: number, attrib: string) => string | null)): Cheerio; /** * 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. * * @category Attributes * @example * * ```js * $('.apple').attr({ id: 'favorite' }).html(); * //=>
  • Apple
  • * ``` * * @param values - Map of attribute names and values. * @returns The instance itself. * @see {@link https://api.jquery.com/attr/} */ export declare function attr(this: Cheerio, values: Record): Cheerio; interface StyleProp { length: number; [key: string]: string | number; [index: number]: string; } /** * Method for getting and setting properties. Gets the property value for only * the first element in the matched set. * * @category Attributes * @example * * ```js * $('input[type="checkbox"]').prop('checked'); * //=> false * * $('input[type="checkbox"]').prop('checked', true).val(); * //=> ok * ``` * * @param name - Name of the property. * @param value - If specified set the property to this. * @returns If `value` is specified the instance itself, otherwise the prop's value. * @see {@link https://api.jquery.com/prop/} */ export declare function prop(this: Cheerio, name: 'tagName' | 'nodeName'): T extends Element ? string : undefined; export declare function prop(this: Cheerio, name: 'innerHTML' | 'outerHTML' | 'innerText' | 'textContent'): string | null; /** Get a parsed CSS style object. */ export declare function prop(this: Cheerio, name: 'style'): StyleProp | 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. * * @example With `baseURI` set to `'https://example.com'`: * * ```js * $('').prop('src'); * //=> 'https://example.com/image.png' * ``` */ export declare function prop(this: Cheerio, name: 'href' | 'src'): string | undefined; /** Get a property of an element. */ export declare function prop(this: Cheerio, name: K): Element[K]; /** Set a property of an element. */ export declare function prop(this: Cheerio, name: K, value: Element[K] | ((this: Element, i: number, prop: K) => Element[keyof Element])): Cheerio; export declare function prop(this: Cheerio, name: Record): Cheerio; export declare function prop(this: Cheerio, name: string, value: string | boolean | null | ((this: Element, i: number, prop: string) => string | boolean)): Cheerio; export declare function prop(this: Cheerio, name: string): string; /** * Method for getting data attributes, for only the first element in the matched set. * * @category Attributes * @example * * ```js * $('
    ').data('apple-color'); * //=> 'red' * ``` * * @param name - Name of the data attribute. * @returns The data attribute's value, or `undefined` if the attribute does not exist. * @see {@link https://api.jquery.com/data/} */ export declare function data(this: Cheerio, name: string): unknown | undefined; /** * Method for getting all of an element's data attributes, for only the first * element in the matched set. * * @category Attributes * @example * * ```js * $('
    ').data(); * //=> { appleColor: 'red' } * ``` * * @returns A map with all of the data attributes. * @see {@link https://api.jquery.com/data/} */ export declare function data(this: Cheerio): Record; /** * Method for setting data attributes, for only the first element in the matched set. * * @category Attributes * @example * * ```js * const apple = $('.apple').data('kind', 'mac'); * * apple.data('kind'); * //=> 'mac' * ``` * * @param name - Name of the data attribute. * @param value - The new value. * @returns The instance itself. * @see {@link https://api.jquery.com/data/} */ export declare function data(this: Cheerio, name: string, value: unknown): Cheerio; /** * Method for setting multiple data attributes at once, for only the first * element in the matched set. * * @category Attributes * @example * * ```js * const apple = $('.apple').data({ kind: 'mac' }); * * apple.data('kind'); * //=> 'mac' * ``` * * @param values - Map of names to values. * @returns The instance itself. * @see {@link https://api.jquery.com/data/} */ export declare function data(this: Cheerio, values: Record): Cheerio; /** * Method for getting the value of input, select, and textarea. Note: Support * for `map`, and `function` has not been added yet. * * @category Attributes * @example * * ```js * $('input[type="text"]').val(); * //=> input_text * ``` * * @returns The value. * @see {@link https://api.jquery.com/val/} */ export declare function val(this: Cheerio): string | undefined | string[]; /** * Method for setting the value of input, select, and textarea. Note: Support * for `map`, and `function` has not been added yet. * * @category Attributes * @example * * ```js * $('input[type="text"]').val('test').html(); * //=> * ``` * * @param value - The new value. * @returns The instance itself. * @see {@link https://api.jquery.com/val/} */ export declare function val(this: Cheerio, value: string | string[]): Cheerio; /** * Method for removing attributes by `name`. * * @category Attributes * @example * * ```js * $('.pear').removeAttr('class').html(); * //=>
  • Pear
  • * * $('.apple').attr('id', 'favorite'); * $('.apple').removeAttr('id class').html(); * //=>
  • Apple
  • * ``` * * @param name - Name of the attribute. * @returns The instance itself. * @see {@link https://api.jquery.com/removeAttr/} */ export declare function removeAttr(this: Cheerio, name: string): Cheerio; /** * Check to see if _any_ of the matched elements have the given `className`. * * @category Attributes * @example * * ```js * $('.pear').hasClass('pear'); * //=> true * * $('apple').hasClass('fruit'); * //=> false * * $('li').hasClass('pear'); * //=> true * ``` * * @param className - Name of the class. * @returns Indicates if an element has the given `className`. * @see {@link https://api.jquery.com/hasClass/} */ export declare function hasClass(this: Cheerio, className: string): boolean; /** * Adds class(es) to all of the matched elements. Also accepts a `function`. * * @category Attributes * @example * * ```js * $('.pear').addClass('fruit').html(); * //=>
  • Pear
  • * * $('.apple').addClass('fruit red').html(); * //=>
  • Apple
  • * ``` * * @param value - Name of new class. * @returns The instance itself. * @see {@link https://api.jquery.com/addClass/} */ export declare function addClass>(this: R, value?: string | ((this: Element, i: number, className: string) => string | undefined)): 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`. * * @category Attributes * @example * * ```js * $('.pear').removeClass('pear').html(); * //=>
  • Pear
  • * * $('.apple').addClass('red').removeClass().html(); * //=>
  • Apple
  • * ``` * * @param name - Name of the class. If not specified, removes all elements. * @returns The instance itself. * @see {@link https://api.jquery.com/removeClass/} */ export declare function removeClass>(this: R, name?: string | ((this: Element, i: number, className: string) => string | undefined)): 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`. * * @category Attributes * @example * * ```js * $('.apple.green').toggleClass('fruit green red').html(); * //=>
  • Apple
  • * * $('.apple.green').toggleClass('fruit green red', true).html(); * //=>
  • Apple
  • * ``` * * @param value - Name of the class. Can also be a function. * @param stateVal - If specified the state of the class. * @returns The instance itself. * @see {@link https://api.jquery.com/toggleClass/} */ export declare function toggleClass>(this: R, value?: string | ((this: Element, i: number, className: string, stateVal?: boolean) => string), stateVal?: boolean): R; export {}; //# sourceMappingURL=attributes.d.ts.map