goldies
    Preparing search index...

    Function attr

    • Gets, sets, or removes an attribute on a DOM element. This is a type-safe wrapper around the native DOM attribute methods.

      Parameters

      • element: Element

        The DOM element to operate on.

      • name: string

        The name of the attribute to get, set, or remove.

      • Optionalvalue: string

        The value to set for the attribute. If null, the attribute will be removed.

      Returns string | Element

      The element's attribute value when getting, or the element itself when setting/removing.

      If element is not a valid DOM element or name is not a string.

      // Get an attribute
      const style = attr(element, 'style');

      // Set an attribute
      attr(element, 'id', 'new-element-id');

      // Remove an attribute
      attr(element, 'data-temp', null);