Search code examples
javascripthtmlhtml-table

Checking to see if an HTML table row element is empty


I am working on developing an html table using JavaScript and I am always creating three rows. However, there is a condition in which it creates two full rows and the last row is an empty row. How can I check to see if the last row is empty before adding it to my table? I do NOT want to add the row if it is empty.

 const element = document.getElementById("two_weeks");
 element.appendChild(tble_row);
 element.appendChild(tble_row2);
 element.appendChild(tble_row3);

Element is my HTML table and tble_row3 is an HTMLTableRowElement.


Solution

  • Shoutout to @Teemu. He came up with the correct answer. This is what I did for it to work:

    if (tble_row3.cells.length) {
         element.appendChild(tble_row3);
    }
    

    This correctly displays rows that have content and does not display rows that are empty.