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:
/
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/nbz4H/1/
Share Improve this question edited Jun 5, 2011 at 0:16 Niklas 30k5 gold badges53 silver badges74 bronze badges asked Jun 4, 2011 at 23:18 Chris AbramsChris Abrams 42.6k20 gold badges54 silver badges57 bronze badges 2-
1
FYI,
find
does not accept a function. Maybe you meantfilter
. – Felix Kling Commented Jun 4, 2011 at 23:25 -
2
Just a pointer, but if you're using jQuery you should select jQuery from the
select
drop-down element on the left of the JS Fiddle demo page... – David Thomas Commented Jun 4, 2011 at 23:26
2 Answers
Reset to default 9Just use a single selector:
$('li[data-label="red"][data-size="1"]').css('color','red');
Example: http://jsfiddle/niklasvh/RyR87/
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"]');