Click here to Skip to main content
13,792,728 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


26 bookmarked
Posted 30 Jul 2006
Licenced CPOL

Dynamically add and remove rows in an HTML table

, 30 Jul 2006
Rate this:
Please Sign up or sign in to vote.
Dynamically add and remove rows in an HTML table using JavaScript.


This article shows you how to add rows to an HTML table dynamically, using DHTML and JavaScript - without requiring a round trip to the web server. I used this technique in a project to build a data entry grid, where the users wanted the flexibility to add or remove rows easily before doing a final submit/save.

The Code

The code has been tested to work with Internet Explorer 5 and above. It may work with other browsers, but I have not tested this.

The sample code has two JavaScript functions - one to add new rows and the other to delete rows. I have kept the page deliberately simple and removed all style and formatting so that the readers are not bogged down with unnecessary information. The functions are simple and self-explanatory.

//add a new row to the table
function addRow()
    //add a row to the rows collection and get a reference to the newly added row
    var newRow = document.all("tblGrid").insertRow();
    //add 3 cells (<td>) to the new row and set the innerHTML to contain text boxes
    var oCell = newRow.insertCell();
    oCell.innerHTML = "<input type='text' name='t1'>";
    oCell = newRow.insertCell();
    oCell.innerHTML = "<input type='text' name='t2'>";
    oCell = newRow.insertCell();
    oCell.innerHTML = "<input type='text' name='t3'>&nbsp;&nbsp;<input" + 
                      " type='button' value='Delete' onclick='removeRow(this);'/>";   
//deletes the specified row from the table
function removeRow(src)
    /* src refers to the input button that was clicked. 
       to get a reference to the containing <tr> element,
       get the parent of the parent (in this case <tr>)
    var oRow = src.parentElement.parentElement;  
    //once the row reference is obtained, delete it passing in its rowIndex   



To use the functions, you need to create an HTML table and give it a unique ID. The sample code uses tblGrid as the ID, but you can use any other name - just make sure to modify the function to use the new name. Next, you would need to modify the addRow function to ensure that the correct number of cells are added and their innerHTML is set properly.

You can try out a working sample of the code at:


This is a simple and effective solution for creating data entry grids where the number of rows to be entered is indeterminate. It relies solely on the client side programming model, thus avoiding server round trips while adding rows. You are not restricted to using simple text boxes for the grid - by changing the innerHTML property of the cells in the addRow function, you can include drop-downs and radio buttons as well.

In a future article, I will show you how to extend the functionality of this grid to include keyboard support and posting the selected text boxes programmatically.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
Eeraj is a software developer working in the USA. He has over 10 years of experience and his interests include ASP.NET, JavaScript, C#, Data Mining and Business Intelligence. You can visit his website at

You may also be interested in...

Comments and Discussions

QuestionHow to insert data in databaseof dynamically created rows ? Pin
Member 137062572-Mar-18 21:56
memberMember 137062572-Mar-18 21:56 
QuestionCan u tell me how to edit/update row Pin
Mahesh Reddy5-Nov-15 0:35
memberMahesh Reddy5-Nov-15 0:35 
GeneralThanks for this post. Pin
Nirmal S Dhar19-Feb-15 3:27
memberNirmal S Dhar19-Feb-15 3:27 
Questionvbscript version Pin
Jhenanne3-Sep-12 2:13
memberJhenanne3-Sep-12 2:13 
GeneralMy vote of 5 Pin
Prashant Trambake24-Dec-10 23:34
memberPrashant Trambake24-Dec-10 23:34 
GeneralMy vote of 1 Pin
balucodeproject113-Jul-10 22:49
memberbalucodeproject113-Jul-10 22:49 
QuestionHow to save the rows value in a database Pin
plm852216-May-10 3:19
memberplm852216-May-10 3:19 
QuestionHow to insert radio button in html table in runtime Pin
Sagar.Vairagade4-Mar-09 1:11
memberSagar.Vairagade4-Mar-09 1:11 
GeneralDynamically add rows - how to format and validate (required entry) Pin
uraidiot1-Nov-08 19:10
memberuraidiot1-Nov-08 19:10 
Generaladding and removing rows dynamically and summing up of rows and columns in javascript Pin
kavuri chaitanya13-Oct-08 7:42
memberkavuri chaitanya13-Oct-08 7:42 
GeneralNice Article. Pin
udaykatakam7-Oct-08 8:27
memberudaykatakam7-Oct-08 8:27 
Generalneed delete row operation for the existed data Pin
Prasanna1125-Jun-08 4:32
memberPrasanna1125-Jun-08 4:32 
Questionadding the data into DB Pin
vkd19806-Jun-08 6:45
membervkd19806-Jun-08 6:45 
Generalthat a nice piece of code!! Pin
Leelavathy5-Jun-08 5:59
memberLeelavathy5-Jun-08 5:59 
Questionhow to store all these records into database? Pin
prashesh30-May-08 2:05
memberprashesh30-May-08 2:05 
GeneralAdding a table dynamically and deleting multiple rows of a table Pin
sumeet_cts22-Apr-08 20:23
membersumeet_cts22-Apr-08 20:23 
Generalthanks!! Pin
smitacharya24-Oct-07 12:18
membersmitacharya24-Oct-07 12:18 
QuestionHow to add new rows to the table that is in the another table Pin
whyIcannotpost23-Feb-07 1:28
memberwhyIcannotpost23-Feb-07 1:28 
AnswerRe: How to add new rows to the table that is in the another table Pin
eeraj28-Feb-07 12:19
membereeraj28-Feb-07 12:19 
QuestionHow to do the same in ASP.NET? Pin
Member 257531712-Feb-07 10:53
memberMember 257531712-Feb-07 10:53 
GeneralHere's an example that worx in firefox Pin
Will667-Jan-07 22:40
memberWill667-Jan-07 22:40 
GeneralGood Article, but.. Pin
Aliasgar_SR22-Nov-06 0:15
memberAliasgar_SR22-Nov-06 0:15 
GeneralRe: Good Article, but.. Pin
eeraj22-Nov-06 4:45
membereeraj22-Nov-06 4:45 
GeneralRe: Good Article, but.. [modified] Pin
HoangKC26-Nov-06 6:47
memberHoangKC26-Nov-06 6:47 
Generalyour code do not support FireFox! :-( Pin
azizi_ra26-Sep-06 7:18
memberazizi_ra26-Sep-06 7:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.181207.3 | Last Updated 30 Jul 2006
Article Copyright 2006 by eeraj
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid