HTML | DOM Table Object

The Table object is used for representing an HTML <table> element. It can be used to create and access a table.

Syntax:

  • To access table element.:
    document.getElementById("id");
  • To create a table object:
    document.createElement("TABLE");

Below program illustrates the Table Object :



Example-1: Accessing a <table> element by using the getElementById() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>Table Object in HTML</title>
    <style>
        table,
        td {
            border: 1px solid green;
        }
          
        h1 {
            color: green;
        }
          
        h2 {
            font-family: Impact;
        }
          
        body {
            text-align: center;
        }
    </style>
</head>
  
<body>
  
    <h1>GeeksforGeeks</h1>
    <h2>Table Object</h2>
    <br>
  
    <table id="table" align="center">
        <tr>
            <td>Fork Python</td>
            <td>Fork Java</td>
        </tr>
        <tr>
            <td>Sudo Placement</td>
            <td>Fork CPP</td>
        </tr>
    </table>
  
    <p>Double click the "Delete Row" 
      button to remove the second row from the table.</p>
  
    <button onclick="delete()">Delete Row</button>
  
    <script>
        function delete() {
            
            // Accessing table object.
            var x = document.getElementById("table");
            x.deleteRow(0);
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:
Before Clicking The Button:

After Clicking The Button:

Example-2: Creating a <table> element by using the document.createElement() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>Table Object in HTML</title>
    <style>
        table {
            border: 1px solid green;
        }
          
        h1 {
            color: green;
        }
          
        h2 {
            font-family: Impact;
        }
          
        body {
            text-align: center;
        }
    </style>
</head>
  
<body>
  
    <h1>GeeksforGeeks</h1>
    <h2>Table Object</h2>
    <br>
  
    <p>Double click the "Create" button to create a 
      TABLE, a Table row and a Table cell element.</p>
  
    <button ondblclick="create()">Create</button>
  
    <script>
        function create() {
            
            // Create table object.
            var a = document.createElement("TABLE");
            a.setAttribute("id", "MyTable");
            document.body.appendChild(a);
  
            var b = document.createElement("TR");
            b.setAttribute("id", "MyTr");
            document.getElementById("MyTable").appendChild(b);
  
            var c = document.createElement("TD");
            var d = document.createTextNode("Table cell");
            c.appendChild(d);
            document.getElementById("MyTr").appendChild(c);
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:
Before Clicking The Button:

After Clicking The Button:

Supported Browsers:

  • Opera
  • Internet Explorer
  • Google Chrome
  • Firefox
  • Apple Safari


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.