@endeavorance/palette
    Preparing search index...

    Class Attributes

    Helper class for reading, writing, and inspecting HTML Element attributes

    While Element.getAttributeNode() and Element.attributes exist, they're quite obtuse when it comes to general web application development. This class follows patterns established by classes such as Headers or URLSearchParams.

    Constructors

    Accessors

    • get record(): Record<string, string | null>

      A deep copy of the internal record of attributes

      Returns Record<string, string | null>

    Methods

    • Apply the current attributes to a single HTMLElement target

      Parameters

      • element: HTMLElement

      Returns void

    • Delete an attribute from this instance

      Parameters

      • key: string

      Returns void

    • Returns MapIterator<[string, string | null]>

      An iterator of the entries of the internal map of attributes

    • Get an attribute value as a string or null if it is not defined

      Parameters

      • key: string

      Returns string | null

    • Get an attribute as a boolean.

      Parameters

      • key: string

      Returns boolean

      If the attribute is present with any non-null value (including the empty string), returns true. Otherwise counts as false.

    • Get an attribute value parsed as a number

      Parameters

      • key: string

      Returns number | null

      The value must be parseable using the Number wrapper. Any value that results in a NaN when parsed will instead return null.

    • Check if a given attribute is known to this instance

      Parameters

      • key: string

      Returns boolean

    • Set an attribute value on the internal record of this instance.

      Parameters

      • key: string
      • value: unknown

      Returns void

      This does NOT update any HTML element, even one used as the source. To apply an instance of Attributes to an HTML element, use the static or instance apply method.

    • Read an attribute value from a target element

      Parameters

      • target: HTMLElement
      • name: string

      Returns string | null

    • Read an attribute from an element as a string or null if it is not set

      Parameters

      • target: HTMLElement
      • name: string
      • type: "string"

      Returns string | null

    • Read an attribute from an element as a boolean or null if it is not set

      Parameters

      • target: HTMLElement
      • name: string
      • type: "boolean"

      Returns boolean

    • Read an attribute from an element as a number or null if it is not set or if the value parses to NaN when parsed with Number()

      Parameters

      • target: HTMLElement
      • name: string
      • type: "number"

      Returns number | null

    • Check if a string is a valid attribute name

      Parameters

      • name: string

      Returns boolean

    • Encode any value as a string or null for use in an AttributeRecord

      Parameters

      • value: unknown

      Returns DOMAttributeValue

    • Set an attribute value, or remove it if the value is null

      Parameters

      • target: HTMLElement
      • name: string
      • value: unknown

      Returns void