title: Create table using Javascript
body: <p>I have a JavaScript function which creates a table with 3 rows 2 cells.</p>
  
  <p>Could anybody tell me how I can create the table below using my function (I need to do this for my situation)?</p>
  
  <p>Here is my javascript and html code given below:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function tableCreate() {
    //body reference 
    var body = document.getElementsByTagName("body")[0];
  
    // create elements &lt;table&gt; and a &lt;tbody&gt;
    var tbl = document.createElement("table");
    var tblBody = document.createElement("tbody");
  
    // cells creation
    for (var j = 0; j &lt;= 2; j++) {
      // table row creation
      var row = document.createElement("tr");
  
      for (var i = 0; i &lt; 2; i++) {
        // create element &lt;td&gt; and text node 
        //Make text node the contents of &lt;td&gt; element
        // put &lt;td&gt; at end of the table row
        var cell = document.createElement("td");
        var cellText = document.createTextNode("cell is row " + j + ", column " + i);
  
        cell.appendChild(cellText);
        row.appendChild(cell);
      }
  
      //row added to end of table body
      tblBody.appendChild(row);
    }
  
    // append the &lt;tbody&gt; inside the &lt;table&gt;
    tbl.appendChild(tblBody);
    // put &lt;table&gt; in the &lt;body&gt;
    body.appendChild(tbl);
    // tbl border attribute to 
    tbl.setAttribute("border", "2");
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table width="100%" border="1"&gt;
    &lt;tr&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td rowspan="2"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>