javascript - jQuery: How can I loop over a set of elements finding only those matching the values within another array? -
i've got little function called finditem()
supposed finding elements i'm looking based on custom data-
attributes on element.
in case these purely numerical eg. data-slide=1
.
i'm bit clueless how match value of each item's data-slide 1 contained within other array.
here more concrete example:
function finditem(count) { var collection = []; $.each(allmyliitems, function(i, item) { if ( $(item).data('slide') == count ) { collection.push(item); } }); return $(collection); } finditem([1,3])
which not work because count
inside if statement not seem match anything.
the page contain 4 <li data-slide="{number}">…
elements 1,3 should return first , third of elements.
what doing wrong here?
use jquery.grep
, jquery.inarray
:
function finditem(items) { return jquery.grep($('li'), function(element, index) { return jquery.inarray($(element).data('slide'), items) != -1; }); }
Comments
Post a Comment