put elements of a table in a list

1

I have an empty table, and then a javascript code that allows you to populate the table via input. What I want is to put all the information in this table into an array (regardless of the number of rows and columns in the table) so that later you can delete elements in the array and automatically change the table.

HTML                        

<input type="text" name="nome" id="nomedisciplina">    
<button onclick="adicionar()">Adicionar disciplina</button><br>

JAVASCRIPT

function adicionar() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    cell1.innerHTML = document.getElementById("nomedisciplina").value;
}

    
asked by anonymous 11.11.2016 / 22:09

1 answer

2

You can do this:

TABLE:

<table id="dataTable">
    <tr>
      <th>Codigo</th>
      <th>Descricao</th>
    </tr>
    <tr>
      <td>1</td>
      <td>José</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Maria</td>
    </tr>
</table>

Javascript:

var table = document.getElementById( "dataTable" );
var tableArr = [];
for ( var i = 1; i < table.rows.length; i++ ) {
    tableArr.push({
        codigo: table.rows[i].cells[0].innerHTML,
        descricao: table.rows[i].cells[1].innerHTML
    });
}
console.log(tableArr);

See jsFiddle: link

    
11.11.2016 / 23:26