How do I select elements on multiple attribute values

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)?


Solution 1:

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

Solution 2:

You could for example chain and filter like so

var elements = $('div[attr1="value1"]').filter('div[attr2="value2"]');

Solution 3:

Find this solution quite simple.

$('[attr1="home"][attr2="settings"]')