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.

    <td class='some-class'>A</td>
    <td class='some-class'>B</td>
    <td class='some-class'>C</td>
    <td class='some-class'>A1</td>
    <td class='some-class'>B2</td>
    <td class='some-class selected'>C3</td>
    <td class='some-class'>A21</td>
    <td class='some-class'>B22</td>
    <td class='some-class'>C23</td>


The current way I am doing is very similar:

found = false;

     if ($(this).hasClass('selected')){
        found  = true;
     if (found){
           # do something



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




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



All Articles