---
title: "Append a row in a table trough javascript"  
description: "Append a row in a table trough javascript"  
author: "Anonymous User"  
published: 2021-07-20  
updated: 2023-11-28  
canonical: https://www.mindstick.com/forum/156571/append-a-row-in-a-table-trough-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# Append a row in a table trough javascript

How to [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) a [row](https://www.mindstick.com/forum/1212/this-row-already-belongs-to-this-table) to [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) on [button click](https://www.mindstick.com/forum/790/form-gets-submiited-twice-on-button-click) via [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?\

## Replies

### Reply by Aryan Kumar

Certainly! You can append a new row to an HTML table using JavaScript. Here's an example:

```plaintext
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Append Row to Table in JavaScript</title>
</head>
<body>

  <table id="myTable" border="1">
    <thead>
      <tr>
        <th>Name</th>
        <th>Age</th>
        <th>City</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>25</td>
        <td>New York</td>
      </tr>
      <tr>
        <td>Jane</td>
        <td>30</td>
        <td>Los Angeles</td>
      </tr>
    </tbody>
  </table>

  <button onclick="appendRow()">Add Row</button>

  <script>
    function appendRow() {
      // Get the reference to the table
      var table = document.getElementById("myTable").getElementsByTagName('tbody')[0];

      // Create a new row
      var newRow = table.insertRow(table.rows.length);

      // Insert cells into the new row
      var cell1 = newRow.insertCell(0);
      var cell2 = newRow.insertCell(1);
      var cell3 = newRow.insertCell(2);

      // Add data to the cells
      cell1.innerHTML = "New Name";
      cell2.innerHTML = "New Age";
      cell3.innerHTML = "New City";
    }
  </script>

</body>
</html>
```

In this example:

1. The table has an existing structure with headers and some rows.
2. The **appendRow** function is called when the "Add Row" [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) is clicked.
3. Inside the function, a reference to the table's **<tbody>** is obtained.
4. A new row (**<tr>**) is created using **insertRow()** on the table's **<tbody>**.
5. Cells (**<td>**) are added to the new row using **insertCell()**.
6. Data is added to the cells using the **innerHTML** property.

When you [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) the "Add Row" button, a new row will be appended to the table with the specified data. You can customize this example based on your specific requirements.


---

Original Source: https://www.mindstick.com/forum/156571/append-a-row-in-a-table-trough-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
