Search code examples
jqueryhtmlcssjqgridcss-tables

how to style certain td in every 3rd table row?


How to select every 4td in every 3rd row?

I have a jQgrid chart and id like to target the client column for every 3rd row starting with first one.

Here is an example: http://jsfiddle.net/ZHRaD/13/

Basic html:

<tr>
   <td></td>
   <td></td>
   <td></td>
   <td></td> // <<
   <td></td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
</tr>
<tr>
   <td></td>
   <td></td>
   <td></td>
   <td></td> // <<
   <td></td>
</tr>

....

Solution

  • You can use rows property of <table> DOM to access <tr> elements and cells property of <tr> to access <td> elements. The corresponding code can look like

    loadComplete: function () {
        var rows = this.rows, cRows = rows.length, iRow, row,
            iSelRows = 0;
        for (iRow = 0; iRow < cRows; iRow++) {
            row = rows[iRow]; // row.id is the rowid
            if ($(row).hasClass("jqgrow")) {
                // the row is a standard row
                if (iSelRows%4 === 0) {
                    $(row.cells[3]).addClass("ui-state-highlight");
                }
                iSelRows++;
            }
        }
    }
    

    The corresponding modified jsfiddle demo is here.