Show array in table

1

How can I do to display an array in javascript in an HTML table?

<body>
<p id="cliente"></p>    
    <script>
        var client = ["10001500", "Benito", "sdfdsadasfl.com", "C/Falsa123"];
        document.getElementById("cliente").innerHTML = client;
    </script>
</body>
    
asked by Iron Man 12.02.2018 в 10:55
source

1 answer

0

To generate an HTML table with JS, you can use the following code:

function crearTabla(datosTabla) {
  var tabla = document.createElement('table');
  var cuerpoTabla = document.createElement('tbody');

  datosTabla.forEach(function(datosFilas) {
    var fila = document.createElement('tr');

    datosFilas.forEach(function(datosCeldas) {
      var celda = document.createElement('td');
      celda.appendChild(document.createTextNode(datosCeldas));
      fila.appendChild(celda);
    });

    cuerpoTabla.appendChild(fila);
  });

  tabla.appendChild(cuerpoTabla);
  document.body.appendChild(tabla);
}
<button onclick="crearTabla([['10001500', 'Benito', 'benito.com', 'C/Falsa123'], ['10047500', 'Dianita', 'dianita.com', 'C/Verdadera789']]);">Crear Tabla</button>
    
answered by 12.02.2018 / 11:08
source