Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
Creating a timetable using JavaScript

I am trying to create a web page where user can create his own schedule. User can enter the values of lines and columns in some input to build a table in which the user will write his schedule. I use this javascript code:

  var p = document.getElementById('paragraph');
  var table = document.createElement('table');
  var tbody = document.createElement('tbody');
  table.appendChild(tbody);

  for (let i = 0; i < lines; i++){
    let tr = document.createElement('tr');
    for (let j = 0; j < columns; j++){
     let td = document.createElement('td');
    }
    tbody.appendChild(tr);
  }

  p.appendChild(table);

However, when I'am trying to add information to table cells, I can't write values to each of them. I've used .innerHTML but it doesn't work the way it needs to. The information will be written only to the beginning of the table.

Should I give id to each td and then address to them by id when I need to write the information? Or there is another way to write values to table cells?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I think you need something like this to insert the data.

We have insertRow(), which is pre-defined function which i used in this answer to insert new row and then inserted columns in it with insertCell function.

<!DOCTYPE html>
<html>

<body>
  <div class="inputs">
    <input type="text" id="input1" placeholder="Enter first col data" >
    <input type="text" id="input2" placeholder="Enter second col data" >
  </div>
  <br>  <br>  <br>
  <table id="myTable">
    <thead style="background-color: beige;">
      <tr>
        <td>default head row cell 1</td>
        <td>default head row cell 2</td>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
  <br>
  <button type="button" onclick="myFunction()">add data to body row</button>
  <script>
  
    function myFunction() {

      var table = document.querySelector("#myTable tbody");
        
      var row = table.insertRow();

      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);


        const val1 = document.getElementById('input1').value;
                const val2 = document.getElementById('input2').value;
      cell1.innerHTML = val1;
      cell2.innerHTML = val2;
    }
  </script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think that you need to refer to your button in the js file and write a function that will be executed on the "onclick" event In this function, you are accessing the table variable. By using the built in javaScript function «insertRow()» you are adding rows to your table. Then you should add cells to this row in which information that users entered will be stored. This you can also do by using build in function «insertCell()» Next, you access the fields in which the user has entered data Retrieve values ​​using the «value» built-in function Using the built-in «innerHTML» function, draw cells with the information that you received in the previous step You can look at the written code below for better assimilation of information

<!DOCTYPE html>
<html>
<body>
  <div class="inputs" >
    <input type="text" id="firstColumn" placeholder="Enter data here" >
    <input type="text" id="SecondColumn" placeholder="Enter data here" >
  </div>
  <table id="Table">
    <thead>
      <tr>
        <td style="background-color: pink;">Name of first column</td>
        <hr>
        <td style="background-color: purple;">Name of second column</td>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
  <br>
  <button style="background-color: yellow;" type="button" id = "btn">Add</button>
<script>
    const button = document.querySelector('#btn');
btn.onclick = function() {
      var table = document.querySelector("#Table tbody");
        
      var row = table.insertRow();

      var Fcell = row.insertCell(0);
      var Scell = row.insertCell(1);


        const Fdata = document.getElementById('firstColumn').value;
                const Sdata = document.getElementById('SecondColumn').value;
      Fcell.innerHTML = Fdata;
      Scell.innerHTML = Sdata;
    }
  </script>
</body>

</html>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda