Selector.withAttribute Method

Finds elements with the specified attribute or attribute value.

Selector().withAttribute(attrName [, attrValue]) → Selector
Argument Type Description
attrName String | RegExp Attribute name (case-sensitive).
attrValue (optional) String | RegExp Attribute value (case-sensitive, optional).

If the argument is a string, the method only returns strict matches.

Examples

// Selects label elements that have the 'for' attribute.
// This attribute can have any value.
const elWithAttrName = Selector('label').withAttribute('for');

// Selects label elements with the 'for' attribute that equals 'background-parallel-testing'.
// Ignores label elements with the 'for' attribute' of a different value.
const elWithAttrNameAndValue = Selector('label').withAttribute('for', 'background-parallel-testing');

// Selects input elements with an attribute that matches the "/data-/" regular expression
// and has a value that matches the "/in*-radio/" regular expression.
const elWithRegExpAttr = Selector('input').withAttribute(/data-/, /in.*-radio/);