Use JQuery To Select Multiple Elements With .eq()
I want to select a subset of tds from a table. I know before hand what the indexes are, but they are effectively random (not odd or even indexes, etc). For instance say I want to s
Solution 1:
I'd do it with .filter()
and $.inArray()
:
var elements = $("table td").filter(function(i) {
return $.inArray(i, indexesToSelect) > -1;
});
Another [more ugly] way is mapping to a selector:
var elements = $($.map(indexesToSelect, function(i) {
return "td:eq(" + i + ")";
}).join(","), "table");
Solution 2:
I wrapped VisioN's filter method into a jQuery plugin:
$.fn.eqAnyOf = function (arrayOfIndexes) {
return this.filter(function(i) {
return $.inArray(i, arrayOfIndexes) > -1;
});
};
So now usage is nice and clean:
var $tds = $('table td').eqAnyOf([1, 5, 9]);
Solution 3:
try this
$('table td:eq(0), table td:eq(5), table td:eq(9)')
Solution 4:
$('table td').filter(':eq(' + indexesToSelect.join('), :eq(') + ')')
Post a Comment for "Use JQuery To Select Multiple Elements With .eq()"