javascript - Use jQuery to select multiple elements with .eq() -
i want select subset of tds table.
i know before hand indexes are, random (not odd or indexes, etc).
for instance want select 0th, 5th , 9th td.
indexestoselect = [0, 5, 9]; // 1) selects 1 one $('table td').eq(0) $('table td').eq(5) $('table td').eq(9) // 2)this selects them group (with underscore / lodash) var $myindexes = $(); _.foreach(indexestoselect, function (idx) { $myindexes = $myindexes.add($('table td').eq(idx)); });
so (2) works , using that, wonder if there more natural way using jquery.
something passing .eq() array of indexes? (that doesn't work)
// not work $('table td').eq([0, 5, 9])
if not write small plugin .eqmulti(array).
note: there no class these tds share exclusively, selecting based on class won't work.
i'd .filter()
, $.inarray()
:
var elements = $("table td").filter(function(i) { return $.inarray(i, indexestoselect) > -1; });
another [more ugly] way mapping selector:
var elements = $($.map(indexestoselect, function(i) { return "td:eq(" + + ")"; }).join(","), "table");