Open In App

JavaScript - How to Add, Edit and Delete Data in HTML Table?

Last Updated : 26 Nov, 2024
Comments
Improve
Suggest changes
8 Likes
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 Data

  • The 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.

Output


Next Article
Article Tags :

Similar Reads