Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery : how to intersect two data-attribute selector queries

Tags:

jquery

I've got some DOM elements with attributes data-foo and data-bar.

Is there an elegant way to return only those elements that match on both attributes ?

At the moment I'm just using a filter, but maybe there's a better way

var result = $('[data-foo="aaa"]').filter('[data-bar="bbb"]');
like image 298
Petrov Avatar asked Aug 22 '13 01:08

Petrov


2 Answers

Just join both selectors together

var result = $('[data-foo="aaa"][data-bar="bbb"]');
like image 165
Musa Avatar answered Oct 22 '22 02:10

Musa


Just tack it on after the first one:

$('[data-foo="aaa"][data-bar="bbb"]');

http://jsfiddle.net/NaHwb/

like image 26
Leonardo Gonzalez Avatar answered Oct 22 '22 03:10

Leonardo Gonzalez