1function generate_table() {
2 // get the reference for the body
3 var body = document.getElementsByTagName("body")[0];
4
5 // creates a <table> element and a <tbody> element
6 var tbl = document.createElement("table");
7 var tblBody = document.createElement("tbody");
8
9 // creating all cells
10 for (var i = 0; i < 2; i++) {
11 // creates a table row
12 var row = document.createElement("tr");
13
14 for (var j = 0; j < 2; j++) {
15 // Create a <td> element and a text node, make the text
16 // node the contents of the <td>, and put the <td> at
17 // the end of the table row
18 var cell = document.createElement("td");
19 var cellText = document.createTextNode("cell in row "+i+", column "+j);
20 cell.appendChild(cellText);
21 row.appendChild(cell);
22 }
23
24 // add the row to the end of the table body
25 tblBody.appendChild(row);
26 }
27
28 // put the <tbody> in the <table>
29 tbl.appendChild(tblBody);
30 // appends <table> into <body>
31 body.appendChild(tbl);
32 // sets the border attribute of tbl to 2;
33 tbl.setAttribute("border", "2");
34}