Create a Table Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Table


You can create a <table> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
table, td {
    border: 1px solid black;
</style>//from ww w  .  java 2s .  co  m

<table id="myTable">
    <td>cell 1</td>
    <td>cell 2</td>
    <td>cell 3</td>
    <td>cell 4</td>

<button onclick="myFunction()">create a thead element for the table</button>

function myFunction() {
    var table = document.getElementById("myTable");
    var header = table.createTHead();
    var row = header.insertRow(0);
    var cell = row.insertCell(0);
    cell.innerHTML = "<b>This is a table header</b>";


Related Tutorials