-1

How can I read all the td cells from the certain column if I know which row to read.

Let me illustrate this:

<tr>
    <td><input type="checkbox" value="0"></td>
    <td>1</td>
    <td>John</td>
    ....
</tr>

At some point I know that I need to read value 0 and I want to know all of the other td in that row.

So in some loop I have this:

selected[i][0]

which is giving me : <input type="checkbox" value="0"> var i is going from 0

How to get all the items?

I have tried with : $('tr').eq(i).find('td').eq(1).text()

and it is not giving me the right values.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user123_456
  • 5,635
  • 26
  • 84
  • 140

5 Answers5

2

Protip: Learn to use the awesome Firebug console to get direct output of selected elements - that's the way to intuitive code Javascript :-)

$('tr').eq(i).children('td').children('input').val() 

maybe, but that's your turn.

enter image description here

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Henry Ruhs
  • 1,533
  • 1
  • 20
  • 32
1

Please see if the following helps, http://jsfiddle.net/sLBgY/

<script>
    function load(){
        var table = document.getElementById("mtable");
        for (var i = 0, row; row = table.rows[i]; i++) {
           if(table.rows[i].cells[0].getElementsByClassName('valTd')[0].value==0){
               for (var j = 1, col; col = row.cells[j]; j++) {
                 alert(table.rows[i].cells[j].innerText);
               } 
            }
        }
    }
</script>

<body onload="load()">
<table id="mtable">    
    <tr>
        <td><input class='valTd' type="checkbox" value="0"></td>
        <td>1</td>
        <td>John</td>
   </tr>
   <tr>
        <td><input class='valTd' type="checkbox" value="1"></td>
        <td>1</td>
        <td>John</td>
   </tr>
 </table>
 </body>
user2580076
  • 577
  • 2
  • 10
  • @user123_456 see if this helps, pure JS – user2580076 Jul 14 '13 at 19:54
  • 1
    Good, just instead of the [innerText](http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox) use something more compatible, and also better compatible than `.getElementsByClassName` is here `.getElementsByTagName` or `.querySelector`: http://jsfiddle.net/sLBgY/1/ – Stano Jul 14 '13 at 20:27
0

if your html will look like this

<tr>
    <td><input type="checkbox" value="0"></td>
    <td>1</td>
    <td>John</td>
</tr>

<tr>
    <td><input type="checkbox" value="0"></td>
    <td>2</td>
    <td>Julie</td>
</tr>

then you would simply do this

   $('tr').each(function( index, element) {
      var $element = $(element),
          checkbox = $element.children(0)[0];
          id = $element.children(1); 
          name = $element.children(2);
   });

Demo Link

iConnor
  • 19,997
  • 14
  • 62
  • 97
0

This will find all the cells in the correct row:

$(function () {
    var found = false;
    $('table tr').each(function () {
        var cells = $(this).children('td');
        if (cells.eq(0).find('input').val() == 0) { // the value you're looking for
            found = cells;
        }
    });
    if (found) {
        alert(found.eq(0).html() + found.eq(1).html() + found.eq(2).html());
    }
});

http://jsfiddle.net/7VmR9/16/

Stano
  • 8,749
  • 6
  • 30
  • 44
0

At some point I know that I need to read value 0 and I want to know all of the other td in that row.

If I am understanding you correctly, this should give you your intended results using the jQuery library:

$("td input[value='0']").parent().parent().children("td").each(function(){
    alert($(this).text()); //Replace alert with however you want to output your data
});

Explanation:

$("td input[value='0']") will select all <input /> tags with the attribute value set to 0, contained within a <td> tag

.parent() will go to the parent <td> tag

.parent() will go to the parent <tr> tag

.children("td") will select all of the <td> tags contained within the respective tr tag

.each(function(){...}) will do something with those <td> tags

$(this).text() will select the <td>'s text

Lil' Bits
  • 898
  • 2
  • 9
  • 24