1

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?

Artem
  • 11
  • 2
  • Related question: [How do I change the text of a span element using JavaScript?](https://stackoverflow.com/questions/1358810/how-do-i-change-the-text-of-a-span-element-using-javascript) – DBS Jan 13 '22 at 09:54
  • Welcome to Stack Overflow! Visit the [help], take the [tour] to see what and [ask]. Please first ***>>>[Search for related topics on SO](https://www.google.com/search?q=javascript+timetable++site%3Astackoverflow.com)<<<*** and if you get stuck, post a [mcve] of your attempt, noting input and expected output using the [`[<>]`](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Jan 13 '22 at 10:02

2 Answers2

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>
DnD2k21
  • 221
  • 1
  • 6
  • 25
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>