Pass through column cells (not row cells) using jQuery (or js) on html tables?

With jQuery, you can simply scroll through cells or rows, but not just scroll through column cells.

//for cells of rows I will do this $('table tr').each(function(index,elem)...//loop through cell of row [index] 

Does anyone suggest a simple method for looping through column cells?

+3
source share
2 answers

Edit: I am not reading the original question correctly. This example will go through all the cells in the table, first ordered by their cells.

Markup

 <table class='sortable'> <tr> <td>a</td> <td>d</td> <td>g</td> </tr> <tr> <td>b</td> <td>e</td> <td>h</td> </tr> <tr> <td>c</td> <td>f</td> <td>i</td> </tr> </table> 

JQuery

 var cells = $('table.sortable td').sort(function(a, b) { //compare the cell index var c0 = $(a).index(); var c1 = $(b).index(); if (c0 == c1) { //compare the row index if needed var r0 = $(a).parent().index(); var r1 = $(b).parent().index(); return r0 - r1; } else return c0 - c1; }); //console.log(cells); cells.each(function() { console.log($(this).html()); }); 

Result:

 a b c d e f g h i 
+3
source
 $(".table_identifier tr > :nth-child(1)").each(function(index,elem)..... 

change 1 to any column you want to select

+3
source

All Articles