How to add rows to a table using JavaScript DOM?

HTMLProgramming ScriptsJavascript

To add rows to a table with JavaScript, use the DOM method insertRow().


You can try to run the following code to learn how to add a row to a table one at a time −

Live Demo

<!DOCTYPE html>
         function rowFunction(x) {
            var a = document.getElementById(x).insertRow(0);
            var b = a.insertCell(0);
            var c = a.insertCell(1);
            b.innerHTML = c.innerHTML = "row";
      <table style="border:2px solid black" id="newtable">
         <input type="button" onclick="rowFunction('newtable')" value="Add a row">
Updated on 23-Jun-2020 07:42:20