Select values within a table by rows

0

How can I select values from an html table by rows in JavaScript ?, I have the following function already done but that brings me together all the rows

    function iiid(e) {
    var iii = "";
    $("td").parent("tr").find("td").each(function () {
        iii += $(this).html() + ",";
    });
    return iii;
}
    
asked by Yoelvis Oliveros 29.06.2017 в 17:09
source

2 answers

1

Instead of selecting all the cells and looping them, you can make two nested loops ( each ): one for the rows and one for the cells in the row:

function iiid(e) {
  var result = [];
  $('tr').each(function(){
    var fila = [];
    $(this).find('td').each(function(){
      fila.push($(this).html());
    });
    result.push(fila.join());
  });
  return result;
}

$(function(){
  $('#leerFilas').click(function(){
    var data = iiid();
    console.log(data);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
  <td>1.1</td>
  <td>1.2</td>
  <td>1.3</td>
  <td>1.4</td>
  <td>1.5</td>
  <td>1.6</td>
</tr>
<tr>
  <td>2.1</td>
  <td>2.2</td>
  <td>2.3</td>
  <td>2.4</td>
  <td>2.5</td>
  <td>2.6</td>
</tr>
<tr>
  <td>3.1</td>
  <td>3.2</td>
  <td>3.3</td>
  <td>3.4</td>
  <td>3.5</td>
  <td>3.6</td>
</tr>
</table>
<button id="leerFilas">Leer filas</button>
    
answered by 29.06.2017 / 17:17
source
0

With this function you add the values of each column by rows of your table

function sumaColumnasTabla(){
    var total = 0;
    $("#idDetuTabla").find("tr").each(function(){
         $(this).find("td").each(function(){
                total+=parseFloat($(this).text());
        });
    });
    return total;
}
    
answered by 30.06.2017 в 23:54