JavaScript - How to Add, Edit and Delete Data in HTML Table? Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To add edit and delete features in an HTML table with JavaScript, create buttons for each action. Use JavaScript functions to add new rows, edit existing data, and remove rows as needed.Approach to Add Edit and Delete DataThe addData() method obtains the data from the input fields and creates a fresh row in the table containing the supplied information.The "Edit" button triggers the editData() function, which replaces name and email table cells with pre-filled input fields. The button's text changes to "Save," and it calls the saveData() function when clicked.The saveData() function saves the edited data by retrieving input values, updating table cells, resetting the button, and reinvoking editData().When the "Delete" button is clicked, the deleteData() function eliminates the associated row from the table.Example: In this example, we will see how to add edit, and delete data in an HTML table using JavaScript. First, we take input from the user then whatever data is entered by the user it will show on the table. HTML <!DOCTYPE html> <html lang="en"> <head> <style> h1, h2 { text-align: center; } #formContainer { text-align: center; margin-bottom: 20px; } label { display: block; margin-top: 10px; } input, textarea { width: 300px; padding: 8px; margin-top: 5px; border: 1px solid #ccc; border-radius: 4px; } button { margin: 10px; padding: 8px 16px; background-color: #4CAF50; color: #fff; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #45a049; } table { border-collapse: collapse; margin-bottom: 20px; width: 100%; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #4CAF50; color: #fff; } </style> </head> <body> <h2>User Data Management</h2> <div id="formContainer"> <label for="nameInput"> Name: </label> <input type="text" id="nameInput" placeholder="Enter Name..."> <label for="emailInput"> Email ID: </label> <input type="email" id="emailInput" placeholder="Enter Email..."> <label for="numberInput"> Mobile Details: </label> <input type="text" id="numberInput" placeholder="Enter Mobile Details..."> <label for="addressInput"> Address: </label> <textarea id="addressInput" placeholder="Enter Address..."></textarea> <br> <button onclick="addData()">Add</button> </div> <table id="outputTable"> <tr> <th>Name</th> <th>Email</th> <th>Mobile Details</th> <th>Address</th> <th>Action</th> </tr> </table> <script> function addData() { // Get input values let name = document.getElementById("nameInput").value; let email = document.getElementById("emailInput").value; let mobile = document.getElementById("numberInput").value; let address = document.getElementById("addressInput").value; // Get the table and insert a new row at the end let table = document.getElementById("outputTable"); let newRow = table.insertRow(table.rows.length); // Insert data into cells of the new row newRow.insertCell(0).innerHTML = name; newRow.insertCell(1).innerHTML = email; newRow.insertCell(2).innerHTML = mobile; newRow.insertCell(3).innerHTML = address; newRow.insertCell(4).innerHTML = '<button onclick="editData(this)">Edit</button>' + '<button onclick="deleteData(this)">Delete</button>'; // Clear input fields clearInputs(); } function editData(button) { // Get the parent row of the clicked button let row = button.parentNode.parentNode; // Get the cells within the row let nameCell = row.cells[0]; let emailCell = row.cells[1]; let mobileCell = row.cells[2]; let addressCell = row.cells[3]; // Prompt the user to enter updated values let nameInput = prompt("Enter the updated name:", nameCell.innerHTML); let emailInput = prompt("Enter the updated email:", emailCell.innerHTML); let numberInput = prompt("Enter the updated mobile details:", mobileCell.innerHTML ); let addressInput = prompt("Enter the updated address:", addressCell.innerHTML ); // Update the cell contents with the new values nameCell.innerHTML = nameInput; emailCell.innerHTML = emailInput; mobileCell.innerHTML = numberInput; addressCell.innerHTML = addressInput; } function deleteData(button) { // Get the parent row of the clicked button let row = button.parentNode.parentNode; // Remove the row from the table row.parentNode.removeChild(row); } function clearInputs() { // Clear input fields document.getElementById("nameInput").value = ""; document.getElementById("emailInput").value = ""; document.getElementById("numberInput").value = ""; document.getElementById("addressInput").value = ""; } </script> </body> </html> Output Comment More infoAdvertise with us Next Article JavaScript - How to Add, Edit and Delete Data in HTML Table? saurabhkumarsharma05 Follow Improve Article Tags : HTML JavaScript-Questions Similar Reads How to Append Header to a HTML Table in JavaScript ? JavaScript allows us to dynamically modify the structure of a table based on user interaction or other dynamic events. Here we will use JavaScript to dynamically create a Header row and then append it to the HTML table. ApproachIn this approach, we are using create elements along with DOM manipulati 3 min read How to Insert a Row in an HTML Table in JavaScript ? In JavaScript, we can insert a row dynamically in the HTML table using various approaches. JavaScript provides us with different methods to accomplish this task as listed below. Table of Content Using insertRow() and insertCell()Using insertAdjacentHTML()Using insertRow() and insertCell()In this app 2 min read How to Add Input Field inside Table Cell in HTML ? In HTML, adding input fields inside the table cells can be done to make the application user interactive and useful in the scenarios of building forms, data entry systems, etc. Below are the approaches to add input field inside a Table Cell in HTML: Table of Content Using 'input' tagUsing data Attri 3 min read How to Add Automatic Serial Number in HTML Table ? Adding automatic serial number to an HTML table is a common requirement for displaying data in a structured format. Serial numbers help users to easily identify and refer to individual rows in a table. In this article, we will discuss two methods to add serial numbers to an HTML table i.e. using HTM 3 min read How to dynamically insert id into table element using JavaScript ? This article explains how to dynamically insert "id" into the table element. This can be done by simply looping over the tables and adding "id"s dynamically. Below are the approaches used to dynamically insert id into table elements using JavaScript: Table of Content Using classList ObjectUsing clas 3 min read How to export HTML table to CSV using JavaScript ? Comma Separated Values or CSV is a type of text file where each value is delimited by a comma. CSV files are very useful for the import and export of data to other software applications.Sometimes while developing web applications, you may come into a scenario where you need to download a CSV file co 6 min read How to add table footer in HTML ? To add a table footer on an HTML table you can use the HTML tfoot tag. The footer is required when the developer wants to show the conclusion or the result of the table data in a single row. Syntax: <tfoot> // Table footer contents... </tfoot> Below example will illustrate the concept of 1 min read How to remove all rows from a table in JavaScript ? Given an HTML document that contains an HTML table, the task is to remove all rows from the HTML table. Removing all rows is different from removing a few rows. This can be done by using JavaScript. Here we have two Approaches to removing all rows from a table: Table of Content Using remove() metho 2 min read How to Create Table in HTML? HTML tables are used for organizing and displaying data in a structured format on web pages. Tables are commonly used for product information, presenting data analytics, or designing a pricing comparison chart. Elements of HTML TableTable ElementsDescription<table>The <table> element def 3 min read How to Add Checkbox in HTML Table ? Adding checkboxes to an HTML table can be useful for various purposes, such as selecting multiple rows for batch processing or toggling the state of individual items. In this article, we will explore two methods to add checkboxes to an HTML table i.e. using basic HTML and JavaScript for dynamic inte 2 min read Like