• Attempts to build a CSS selector string from the attributes object. Note that the values of the attributes object are coerced to a string and null excludes a value but only includes a name. If tagName is specified, it is included in the resulting selector.

    Note

    If you specify the optional TN (tag name) generic, you'll get autocomplete on the attributes for the Element associated with that tag name.

    Type Parameters

    • TN extends TagName = "*"

      Tag name of Element to select attributes from.

    Parameters

    • attributes: Attributes<TN>

      Object with key of attribute name and value of attribute value.

    • OptionaltagName: TagName

      Optional tagName name to include in the selector.

    Returns CssSelector

    CSS selector based on the attributes.

    InvalidAttributeError if a value in attributes could not be stringified.

    Single Entry With Value

    selectAttributes({ disabled: true });
    // [disabled="true"]

    Single Entry With Value and Tag

    selectAttributes({ disabled: true }, "button");
    // button[disabled="true"]

    Single Entry Without Value

    selectAttributes({ inert: null });
    // [inert]

    Multiple Entries

    selectAttributes({ disabled: true, inert: null });
    // [disabled="true"][inert]