HTML DOM Table Object


The HTML DOM table Object represent the <table> element of an HTML document.

Let us see how to create table object

Syntax

Following is the syntax −

document.createElement(“TABLE”);

Properties

Following are the properties of table object −

PropertyExplanation
captionIt returns <caption> element of a table in an HTML document.
tFootIt returns <tfoot> element of a table in an HTML document.
tHeadIt returns <thead> element of a table in an HTML document.

Methods

Following are the methods of table object −

MethodExplanation
createCaption()It generates an empty <caption> element and adds it to the table.
createTFoot()It generates an empty <tfoot> element and adds it to the table.
createTHead()It generates an empty <thead> element and adds it to the table.
deleteCaption()It delete the first <caption> element from the table.
deleteRow()It deletes a <tr> element from the table.
deleteThead()It delete the <thead> element from the table.
deleteTFoot()It delete the <tfoot> element from the table.
insertRow()It generates an empty <tr> element and adds it to the table.

Example

Let us see an example of table object −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      text-align: center;
      background-color: #fff;
      color: #0197F6;
   }
   h1 {
      color: #23CE6B;
   }
   .btn {
      background-color: #fff;
      border: 1.5px dashed #0197F6;
      height: 2rem;
      border-radius: 2px;
      width: 60%;
      margin: 2rem auto;
      display: block;
      color: #0197F6;
      outline: none;
      cursor: pointer;
   }
</style>
</head>
<body>
<h1>DOM table Object Demo</h1>
<button onclick="createTable()" class="btn">Create a table object</button>
<script>
   function createTable() {
      var table = document.createElement("TABLE");
      table.innerHTML = `<tr>
      <td>Data 1</td>
      <td>Data 2</td>
      </tr>
      <tr>
      <td>Data 3</td>
      <td>Data 4</td>
      </tr>`;
      table.setAttribute('border', "2");
      table.style.margin = "2rem auto";
      document.body.appendChild(table);
   }
</script>
</body>
</html>

Output

This will produce the following output −

Click on “Create a table object” button to create a table object.

Updated on: 30-Jul-2019

510 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements