Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .find() 2 matching arguments

I have an unordered list with multiple properties per element and I want to find all elements that have both properties.

var results = $('#mylist').find(function() {
    return
        $(this).attr('data-label') == 'red' &&
        $(this).attr('data-size') == 1;
});

I attached an example in the link below:

http://jsfiddle.net/nbz4H/1/

like image 802
Chris Abrams Avatar asked Feb 15 '26 02:02

Chris Abrams


2 Answers

Just use a single selector:

$('li[data-label="red"][data-size="1"]').css('color','red');

Example: http://jsfiddle.net/niklasvh/RyR87/

like image 156
Niklas Avatar answered Feb 16 '26 15:02

Niklas


jQuery's find doesn't take a function as a parameter. That's why this doesn't work.

What you need is to construct an appropriate CSS selector. Something like:

 results = $('#mylist [data-label="red"][data-size="1"]');
like image 32
Dancrumb Avatar answered Feb 16 '26 16:02

Dancrumb