With jQuery, it is easy to select elements with a given attribute value.
For example:
var elements = $('div[attr1="value1"]');
But how do I select on multiple attributes (e.g., attr1 = value1
and attr2 = value2
)?
The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".
A data attribute is a custom attribute that stores information. Data attributes always start with “data-” then followed with a descriptive name of the data that is being stored. You can have multiple data attributes on an element and be used on any HTML element.
Since jquery uses CSS selectors, as defined by the CSS specification a selector with multiple conditions will look like:
$('div[attr1="value1"][attr2="value2"]')
see the CSS spec for further reference: http://www.w3.org/TR/CSS2/selector.html#matching-attrs
You could for example chain and filter like so
var elements = $('div[attr1="value1"]').filter('div[attr2="value2"]');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With