Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

create a html table dynamically using javascript

function generate_table() {
  // get the reference for the body
  var body = document.getElementsByTagName("body")[0];

  // creates a <table> element and a <tbody> element
  var tbl = document.createElement("table");
  var tblBody = document.createElement("tbody");

  // creating all cells
  for (var i = 0; i < 2; i++) {
    // creates a table row
    var row = document.createElement("tr");

    for (var j = 0; j < 2; j++) {
      // Create a <td> element and a text node, make the text
      // node the contents of the <td>, and put the <td> at
      // the end of the table row
      var cell = document.createElement("td");
      var cellText = document.createTextNode("cell in row "+i+", column "+j);
      cell.appendChild(cellText);
      row.appendChild(cell);
    }

    // add the row to the end of the table body
    tblBody.appendChild(row);
  }

  // put the <tbody> in the <table>
  tbl.appendChild(tblBody);
  // appends <table> into <body>
  body.appendChild(tbl);
  // sets the border attribute of tbl to 2;
  tbl.setAttribute("border", "2");
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: react addeventlistener useeffect 
Javascript :: While loop factorial function in javascript 
Javascript :: how to append the dropdown values by jquery each function 
Javascript :: import react icons 
Javascript :: regex separator 
Javascript :: select2 clear fields 
Javascript :: angular add a new line from component 
Javascript :: javascript change paragraph text 
Javascript :: you should not use switch outside a router react 
Javascript :: javascript string contains string 
Javascript :: zoho smtp mail nodejs 
Javascript :: jest writing test 
Javascript :: link on click jquery 
Javascript :: tailwind config 
Javascript :: mongoose delete request 
Javascript :: chrome add a bookmark that appends to current url 
Javascript :: submit a form on enter angular 
Javascript :: if datatable is null js 
Javascript :: check if all elements in array are true javascript 
Javascript :: Bots latency discord js 
Javascript :: javascript clear radio button 
Javascript :: convert string in hh:mm am/pm to date js 
Javascript :: js random numbers 
Javascript :: jquery validation manually trigger 
Javascript :: vuejs bootsrap modal hidden event 
Javascript :: javascript iterate through a map 
Javascript :: vue cli - Uncaught SyntaxError: Unexpected token < 
Javascript :: vowel array 
Javascript :: js named parameters 
Javascript :: vue 3 computed 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =