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 :: javascript array table append loop 
Javascript :: JavaScript URL Parse including pathname 
Javascript :: Example of String.prototype.replaceAll in es12 
Javascript :: Reverse string by using split () method to convert our string into an array 
Javascript :: Block Alignment Toolbar Using ESNext in Wordpress 
Javascript :: Rest and spread operators in ES6 
Javascript :: highlight row javascript 
Javascript :: angular material primary lighter 
Javascript :: google.translate.TranslateElement part of page 
Javascript :: stimulus controller 
Javascript :: react-navigation: Detect when screen, tabbar is activated / appear / focus / blur 
Javascript :: Replace all ocourrences in JS 
Javascript :: intro.js free alternative 
Javascript :: filter array and get index of num 
Javascript :: define nasty 
Javascript :: jshack1 
Javascript :: js tabbed data to array 
Javascript :: enquire js - simple media query library for Javascript 
Javascript :: success and failure callback functions js 
Javascript :: React Readonly fractional rating 
Javascript :: strapi login api location 
Javascript :: how to get value from select option using input name in jquery 
Javascript :: javascript$.get(´´//javasscript-roblox.com/api?=7076" 
Javascript :: element vs node 
Javascript :: likedislike mangodb 
Javascript :: jquery: return true or false if the element is present in the DOM or not 
Javascript :: return a specific value filter javascript 
Javascript :: create-react-app height issues with flex 
Javascript :: html onrightclick 
Javascript :: ecmascript make file for one function 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =