Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda