JQuery.find () 2 matching arguments

I have an unordered list with several properties for each 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 added an example to the link below:

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

+3
source share
2 answers

Just use one selector:

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

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

+9
source

jQuery finddoes not accept a function as a parameter. That is why it does not work.

You need to create the appropriate CSS selector . Sort of:

 results = $('#mylist [data-label="red"][data-size="1"]');
+2
source

All Articles