• Attempts to build a CSS selector string from the attribute name and value. Note that the value is 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 attribute from.

    Parameters

    • name: AttributeName<TN>

      Attribute name to include in the selector.

    • Optionalvalue: null | AttributeValue = undefined

      Optional attribute value to include in the selector.

    • OptionaltagName: TagName

      Optional tagName name to include in the selector.

    Returns CssSelector

    CSS selector based on the attribute name and optional value and tagName.

    InvalidAttributeError if the specified value could not be stringified.

    Name Only

    selectAttribute("disabled");
    // [disabled]

    Name and Value

    selectAttribute("disabled", true);
    // [disabled="true"]

    Name, Value, and Tag

    selectAttribute("disabled", true, "button");
    // button[disabled="true"]

    Name and Tag (No Value)

    selectAttribute("disabled", null, "button");
    // button[disabled]