Click here to Skip to main content
6,822,123 members and growing! (18,651 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Beginner

Dynamically add and remove rows in a HTML table

By eeraj

Dynamically add and remove rows in a HTML table using Javascript
Javascript, Windows, Dev
Posted:30 Jul 2006
Views:113,507
Bookmarked:21 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 3.24 Rating: 3.59 out of 5
2 votes, 25.0%
1
1 vote, 12.5%
2

3

4
5 votes, 62.5%
5

Introduction

This article shows you how to add rows to a 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 unnesessary 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'>  <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   

    document.all("tblGrid").deleteRow(oRow.rowIndex);  

   }

Usage

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 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:

http://www.interviewboard.com/DHTMLSamples/DHTMLGridSample.htm

Conclusion

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

eeraj


Member
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 http://www.interviewboard.com
Occupation: Web Developer
Location: United States United States

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralHow to insert radio button in html table in runtime PinmemberSagar.Vairagade1:11 4 Mar '09  
GeneralDynamically add rows - how to format and validate (required entry) Pinmemberuraidiot19:10 1 Nov '08  
Generaladding and removing rows dynamically and summing up of rows and columns in javascript Pinmemberkavuri chaitanya7:42 13 Oct '08  
GeneralNice Article. Pinmemberudaykatakam8:27 7 Oct '08  
Generalneed delete row operation for the existed data PinmemberPrasanna114:32 25 Jun '08  
Questionadding the data into DB Pinmembervkd19806:45 6 Jun '08  
Generalthat a nice piece of code!! PinmemberLeelavathy5:59 5 Jun '08  
Questionhow to store all these records into database? Pinmemberprashesh2:05 30 May '08  
GeneralAdding a table dynamically and deleting multiple rows of a table Pinmembersumeet_cts20:23 22 Apr '08  
Generalthanks!! Pinmembersmitacharya12:18 24 Oct '07  
QuestionHow to add new rows to the table that is in the another table PinmemberwhyIcannotpost1:28 23 Feb '07  
AnswerRe: How to add new rows to the table that is in the another table Pinmembereeraj12:19 28 Feb '07  
QuestionHow to do the same in ASP.NET? Pinmember10:53 12 Feb '07  
GeneralHere's an example that worx in firefox PinmemberWill6622:40 7 Jan '07  
GeneralGood Article, but.. PinmemberAliasgar_SR0:15 22 Nov '06  
GeneralRe: Good Article, but.. Pinmembereeraj4:45 22 Nov '06  
GeneralRe: Good Article, but.. [modified] Pinmemberchucap6:47 26 Nov '06  
Generalyour code do not support FireFox! :-( Pinmemberazizi_ra7:18 26 Sep '06  
GeneralRe: your code do not support FireFox! :-( PinmemberMBernardo10:57 6 Dec '06  
QuestionRe: your code do not support FireFox! :-( PinmemberSadiaawan7:01 26 Mar '08  
AnswerRe: your code do not support FireFox!. These modifications works in IE/FF Pinmemberbjn222:25 19 Apr '08  
GeneralThis is cool PinmemberKayiwadaniel4:40 31 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 30 Jul 2006
Editor:
Copyright 2006 by eeraj
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project