I’ve got an array of indices (in no particular order) and I need to select all the <tr>s in a table based on these indices.
What I’ve got feels sloppy & inefficient. Is there a better way to write this selector?
For those who like to fiddle: http://jsfiddle.net/PwnhJ/1/
For everyone else:
<table>
<tr>
<td>one</td>
</tr>
<tr>
<td>two</td>
</tr>
<tr>
<td>three</td>
</tr>
<tr>
<td>four</td>
</tr>
<tr>
<td>five</td>
</tr>
<tr>
<td>six</td>
</tr>
<tr>
<td>seven</td>
</tr>
</table>
var indices = [0,3,4,6];
//What I'd love to do
alert($("tr").eq(indices).length); //returns 0
//What does work, but feels sloppy & inefficient
var selector = "";
$(indices).each(function(i, index){
selector += "tr:eq(" + index + ")";
if(i + 1 != indices.length){
selector += ","
}
});
alert($(selector).length); //returns 4
Any help, comments, suggestions would be appreciated.
Thanks!
http://jsfiddle.net/aUmNK/1/