0

This is how my html looks like

<table id="emails-list">
        <tbody>
            
        </tbody>
    </table>

Then using Javascript I want to add rows and columns but I first want to clear anything that is in the tbody so as to not append/stack the same rows and columns everytime the function is called.

function DisplayEmails(item){
  
  //searching for table
  var table = document.querySelector('tbody');

  // somehow clear the tbody of the table here ?????
  
  // adding a row
  var row = table.insertRow(0);
  // adding 3 cells to the row
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  var cell3 = row.insertCell(2);

  cell1.innerHTML = item.sender;
  cell2.innerHTML = item.subject;
  cell3.innerHTML = item.timestamp;
  • 1
    What have you tried, exactly? [`table.replaceChildren();`](//developer.mozilla.org/docs/Web/API/Element/replaceChildren) should work fine. See [Remove all child elements of a DOM node in JavaScript](/a/65413839/4642212). – Sebastian Simon Mar 14 '22 at 21:55
  • It seems to work^ I was trying .clear() and .values = ''; How do I accept the answer? I'm fairly new to stack overflow – Francisco Gutierrez Ramirez Mar 14 '22 at 21:59
  • No need to accept any answer. There is no `clear` method or `values` property on the `HTMLTableSectionElement` prototype. – Sebastian Simon Mar 14 '22 at 22:03

0 Answers0