JQuery - Iterate over subset of tds starting from found class
I'm looking for a more convenient jquery / clean way to be able to select everything td.some-class
and be able to start with 'selected' and apply some for subsequent tds.
<table>
<tr>
<td class='some-class'>A</td>
<td class='some-class'>B</td>
<td class='some-class'>C</td>
</tr>
<tr>
<td class='some-class'>A1</td>
<td class='some-class'>B2</td>
<td class='some-class selected'>C3</td>
</tr>
<tr>
<td class='some-class'>A21</td>
<td class='some-class'>B22</td>
<td class='some-class'>C23</td>
</tr>
The current way I am doing is very similar:
found = false;
$('td.some-class').each(function(){
if ($(this).hasClass('selected')){
found = true;
}
if (found){
# do something
}
});
+3
Nix
source
to share
2 answers
My attempt (this is more efficient than @suhair):
var all = $('#xTable .some-class');
var selected = all.filter('.selected')[0];
var selectedIndex = all.index(selected);
var after = all.filter(':gt(' + selectedIndex + ')');
@suhair Updated DEMO
JSperf
+1
gdoron
source
to share
This is my quick try jsfiddler
var index = $('#xTable td').index($("#xTable td.selected"));
$('#xTable td:gt('+ index+')').css("border", "1px solid red");
Hope this is what you intended
+3
suhair
source
to share