Add, Edit And Delete Rows From Table Dynamically Using CSS and JavaScript

In this article Add, Edit And Delete Rows From Table Dynamically Using CSS and JavaScript

we will show you how to do different operations with tables dynamically like add rows, edit rows, delete rows and save the edited rows on tables using javascript.

Add, Edit And Delete Rows From Table Dynamically Using CSS and JavaScript

Add, Edit And Delete Rows From Table Dynamically Using CSS and JavaScript

Step 1. Add Table to your HTML file and define markup

<script type="text/javascript" src="table_script.js"></script>
<div id="wrapper">
<table align='center' cellspacing=2 cellpadding=5 id="data_table" border=1>

<tr id="row1">
<td id="name_row1">Harish</td>
<td id="country_row1">India</td>
<td id="age_row1">20</td>
<input type="button" id="edit_button1" value="Edit" class="edit" onclick="edit_row('1')">
<input type="button" id="save_button1" value="Save" class="save" onclick="save_row('1')">
<input type="button" value="Delete" class="delete" onclick="delete_row('1')">

<tr id="row2">
<td id="name_row2">John</td>
<td id="country_row2">Canada</td>
<td id="age_row2">26</td>
<input type="button" id="edit_button2" value="Edit" class="edit" onclick="edit_row('2')">
<input type="button" id="save_button2" value="Save" class="save" onclick="save_row('2')">
<input type="button" value="Delete" class="delete" onclick="delete_row('2')">

<tr id="row3">
<td id="name_row3">Laxman</td>
<td id="country_row3">India</td>
<td id="age_row3">19</td>
<input type="button" id="edit_button3" value="Edit" class="edit" onclick="edit_row('3')">
<input type="button" id="save_button3" value="Save" class="save" onclick="save_row('3')">
<input type="button" value="Delete" class="delete" onclick="delete_row('3')">

<td><input type="text" id="new_name"></td>
<td><input type="text" id="new_country"></td>
<td><input type="text" id="new_age"></td>
<td><input type="button" class="add" onclick="add_row();" value="Add Row"></td>



Step 2. Add Table scripting

Add, Edit And Delete Rows From Table Dynamically Using CSS and JavaScript

function edit_row(no)
 var name=document.getElementById("name_row"+no);
 var country=document.getElementById("country_row"+no);
 var age=document.getElementById("age_row"+no);
 var name_data=name.innerHTML;
 var country_data=country.innerHTML;
 var age_data=age.innerHTML;
 name.innerHTML="<input type='text' id='name_text"+no+"' value='"+name_data+"'>";
 country.innerHTML="<input type='text' id='country_text"+no+"' value='"+country_data+"'>";
 age.innerHTML="<input type='text' id='age_text"+no+"' value='"+age_data+"'>";

function save_row(no)
 var name_val=document.getElementById("name_text"+no).value;
 var country_val=document.getElementById("country_text"+no).value;
 var age_val=document.getElementById("age_text"+no).value;



function delete_row(no)

function add_row()
 var new_name=document.getElementById("new_name").value;
 var new_country=document.getElementById("new_country").value;
 var new_age=document.getElementById("new_age").value;
 var table=document.getElementById("data_table");
 var table_len=(table.rows.length)-1;
 var row = table.insertRow(table_len).outerHTML="<tr id='row"+table_len+"'><td id='name_row"+table_len+"'>"+new_name+"</td><td id='country_row"+table_len+"'>"+new_country+"</td><td id='age_row"+table_len+"'>"+new_age+"</td><td><input type='button' id='edit_button"+table_len+"' value='Edit' class='edit' onclick='edit_row("+table_len+")'> <input type='button' id='save_button"+table_len+"' value='Save' class='save' onclick='save_row("+table_len+")'> <input type='button' value='Delete' class='delete' onclick='delete_row("+table_len+")'></td></tr>";


In this step, we made 4 functions to perform the different operations in table.

We will explain all these functions one by one.

In the first function is edit_row function is used to edit rows. In this function, we get row id in ‘no’ variable and then we hide and edit button and display the save button then we get all the data of the edit row and insert the textbox with data in the edit row to make rows editable.

The second function that is save_row function is used to save the edited row. In this function, we get the value of all three text boxes inside edit rows and insert the data in a row when the user clicks on the save button, and then change the display option of the edit and save button.

The third function that is delete_row function is used to delete particular rows when user clicks of delete button of their respective row.

The fourth function that is the add_row function is used to add new rows to the table. In this function we get the value of all the three new textboxes used to insert data then we insert the row in the last-1 position because in our case the last position infilled by our three textboxes that’s why we use -1 to insert the new row before the new textbox row and we clear the three new textbox field.

this is how to Add,Edit, And Delete Rows From Table Dynamically Using JavaScript.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Also Read Add / Remove input field dynamically with jQuery

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *