Create dynamic table in javascript w3schools

❮ Table Object

Example

Insert new row(s) at the first position of a table (and insert a

element with some content to it):

// Find a

element with id="myTable":
var table = document.getElementById("myTable");

// Create an empty

element and add it to the 1st position of the table:
var row = table.insertRow(0);

// Insert new cells (

element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);

// Add some text to the new cells:
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";

Try it Yourself »


Definition and Usage

The insertRow() method creates an empty

element and adds it to a table.

The insertRow() method inserts the new row(s) at the specified index in the table.

Note: A

element must contain one or more tag


❮ Table Object



Table Object

The Table object represents an HTML

elements) at the 1st and 2nd position of the "new"
or elements.

Tip: Use the deleteRow() method to remove a row.


Browser Support

Method
insertRow() Yes Yes Yes Yes Yes

Syntax

tableObject.insertRow(index)

Parameter Values

ValueDescription
index Required in Firefox and Opera, optional in IE, Chrome and Safari. A number that specifies the position of the row to insert (starts at 0). The value of 0 results in that the new row will be inserted at the first position.

The value of -1 can also be used, this results in a new row being inserted at the last position.

This parameter is required in Firefox and Opera, but optional in Internet Explorer, Chrome and Safari.

If this parameter is omitted, insertRow() inserts a new row at the last position in Chrome, IE, Firefox and Opera, and at the first position in Safari.



Technical Details

element
Return Value:The inserted

More Examples

Example

Create and delete row(s):

function myCreateFunction() {
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  cell1.innerHTML = "NEW CELL1";
  cell2.innerHTML = "NEW CELL2";
}

function myDeleteFunction() {
  document.getElementById("myTable").deleteRow(0);
}

Try it Yourself »


HTML reference: HTML

element.

Access a Table Object

You can access a

element by using getElementById():

Create a Table Object

You can create a

element by using the document.createElement() method:

Table Object Collections

elements in a table elements in a table
CollectionDescription
rows Returns a collection of all
tBodies Returns a collection of all


Table Object Properties

element of a table element of a table
PropertyDescription
align Not supported in HTML5. Use style.cssFloat instead.
Sets or returns the alignment of a table according to surrounding text
background Not supported in HTML5. Use style.background instead.
Sets or returns the background image of a table
bgColor Not supported in HTML5. Use style.backgroundColor instead.
Sets or returns the background color of a table
border Deprecated. Use style.border instead.
Sets or returns the width of the table border.
caption Returns the
element of a table
cellPadding Not supported in HTML5. Use style.padding instead.
Sets or returns the amount of space between the cell border and cell content
cellSpacing Not supported in HTML5. Use style.borderSpacing instead.
Sets or returns the amount of space between the cells in a table
frame Not supported in HTML5.
Sets or returns which outer-borders (of a table) that should be displayed
height Not supported in HTML5. Use style.height instead.
Sets or returns the height of a table
rules Not supported in HTML5.
Sets or returns which inner-borders (between the cells) that should be displayed in a table
summary Not supported in HTML5.
Sets or returns a description of the data in a table
tFoot Returns a reference to the
tHead Returns a reference to the
width Not supported in HTML5. Use style.width instead.
Sets or returns the width of the table

Table Object Methods

element and adds it to the table element and adds it to the table ) from the table element from the table element from the table element and adds it to the table
MethodDescription
createCaption() Creates an empty
element and adds it to the table
createTFoot() Creates an empty
createTHead() Creates an empty
deleteCaption() Removes the first
element from the table
deleteRow() Removes a row (
deleteTFoot() Removes the
deleteTHead() Removes the
insertRow() Creates an empty

Standard Properties and Events

The Table object also supports the standard properties and events.


HTML tutorial: HTML Tables

HTML reference: HTML

tag



How do I create a dynamic table in HTML?

Creating a table dynamically (back to Sample1..
Get the body object (first item of the document object)..
Create all the elements..
Finally, append each child according to the table structure (as in the above figure). The following source code is a commented version for the sample1. html..

How can you create a table with 4 rows and 3 columns in HTML?

Creating Tables in HTML You can create a table using the
element. Inside the
element, you can use the elements to create rows, and to create columns inside a row you can use the
elements. You can also define a cell as a header for a group of table cells using the element.

How do you create a table in Dom?

To create a table object: document. createElement("TABLE");

How do you fill a table in JavaScript?

var table = document. getElementById("myTable"); var rowNode = document. createElement("tr"); var cellNode = document. createElement("td"); var textNode = document.