@laserware/dominator
    Preparing search index...

    Function getAttributes

    • Builds an object with the keys equal to the attribute names and the value equal to the corresponding attribute value in the target. If the value is found it is coerced to a boolean if "true" or "false", a number if numeric, or the string value if a string. If not found, the value is null.

      Important

      You will need to perform checks for whether a value is null in the returned object if some of the entries weren't present. See the code block below for additional details.

      // Assuming you pass this in as the generic:
      type ShapeIn = {
      role: string;
      "aria-label": string;
      };

      // The return type of this function is:
      type ShapeOut = {
      role: string | null;
      "aria-label": string | null;
      };

      Type Parameters

      Parameters

      • target: null | Target

        Element, EventTarget, or CSS selector.

      • names: KeysOf<V>

        Names of the attributes for which to find values.

      Returns WithNullValues<V>

      Object with names as keys and corresponding attribute values (or null if not present).

      The arcade!WithNullValues type represents an object with values that could be null.

      elements!InvalidElementError if the specified target wasn't found.

      HTML

      <div
      id="example"
      role="slider"
      aria-valuemax="30"
      aria-label="Example"
      aria-disabled="false"
      >
      ...
      </div>

      Code

      type Shape = {
      "aria-label": string | null;
      "aria-valuemax": number | null;
      invalid: string | null;
      };

      const element = findElement("#example")!;

      getAttributes<Shape>(element, [
      "aria-label",
      "aria-valuemax",
      "invalid", // Doesn't exist, so it's `null`
      ]);
      // { "aria-label": "Example", "aria-valuemax": 30, invalid: null }