views:

1091

answers:

3

i have a table

<table>
<tr>
   <td class="myA">Data...1a</td>
   <td class="myA">Data...2a</td>
   <td class="myA">Data...3a</td>
</tr>
<tr>
   <td class="myB">Data...1b</td>
   <td class="myB">Data...2b</td>
   <td class="myB">Data...3b</td>
</tr>
<tr>
   <td class="myC">Data...1c</td>
   <td class="myC">Data...2c</td>
   <td class="myC">Data...3c</td>
</tr>
</table>

that have we often: data is separated not in one row and their columns it is often that data is in different rows but in same td positions (because connected data should written among themselves)

so i thought if i have the index it is possible to get a class filter and .get(theIndex) on each tr-row to extract the data from the td individual

but is there an easy way? -> i'dl like to extract the data which belongs together in one step -> and afterwards maybe it is in one array/jquery set?!

any idea?! (i do not can add ids etc - i only have the index of the td and the classNames)

A: 

It's really difficult to understand what you are trying to achieve. If you know the particular row which you want to access, you can always do this,

var rowIndex = 1;
$('table tr:eq(' + rowIndex + ') td').each( function() { /* cell for the row identified by rowIndex. */});

You can also access all the tds having the same class name.

$('td.myB').each( function() { } ); //Access all the cells having class 'myB'.
SolutionYogi
how to access the row only and the tds only defined by className is clear but looking between different rows to extract the data belonging to the table td index is the problem
index of the TD is not sufficient to identify a cell uniquely. E.g. index of 2 may mean last cell in first row OR last cell in 3rd row. How to decide? You need additional information to locate the element.
SolutionYogi
ok call itt not index: call it position: from all rows the content of the td with the position 2 (which means if start from 1 -> each middle column)
A: 

I'm not sure if this is what you are looking for, but this function, given a particular table element reference, and zero-based index, will return an array of cells in that particular column. Let me know if this makes sense.

function get_table_column(table, index) {
    cells = []
    $('tr', table).each(function() { 
        cells.push ($('td:eq(' + index + ')', this));
    });

    return cells;        
}
sixthgear
+1  A: 

Here is probably what you are looking for:

<html>
    <head>
    </head>
    <body>
     <table>
      <tr>
       <td class="A">First Name 1</td>
       <td class="A">First Name 2</td>
       <td class="A">First Name 3</td>
      </tr>
      <tr>
       <td class="A">Last Name 1</td>
       <td class="B">Last Name 2</td>
       <td class="B">Last Name 3</td>
      </tr>
     </table>
     <script src="jquery-1.3.2.min.js"></script>
     <script>
     $(function() {
      var idx = 2;
      var data = [];
      $("tr td:nth-child(" + idx +")")
      .each(function() { 
       data[data.length] = $(this).text(); 
      });
      var person = { 
       firstName : data[0], 
       lastName : data[1] };

      alert (person.firstName + " " + person.lastName);
     });
     </script>
    </body>
</html>

The example above will give you firstname 2 and lastname 2 from the multiple rows (td index = 2)

Jimmy Chandra
+1 for nth-child. I forgot about that selector in my answer.
sixthgear
keeping css selector cheat sheet close by :) Comes in handy. Don't remember it on top of my head either.
Jimmy Chandra