With insertRow() method you can insert a new row at the specified position in HTML table. After row is created, use insertCell() method to insert a table cell. Wrap this methods in JavaScript functions and you have code to dynamically add new rows and columns in the HTML table. Please try to click the buttons below …
Small | ||
HTML | ||
table |
When you click the “Add row” button, appendRow() function is called. Function is simple, table row is inserted at the last position, and loop iterates through table cells in the first row. Why first row? Because you have to know the number of columns. For each step (cell in the first row), createCell() function creates and attaches DIV element to the newly created table cell.
// append row to the HTML table function appendRow() { var tbl = document.getElementById('my-table'), // table reference row = tbl.insertRow(tbl.rows.length), // append table row i; // insert table cells to the new row for (i = 0; i < tbl.rows[0].cells.length; i++) { createCell(row.insertCell(i), i, 'row'); } } // create DIV element and append to the table cell function createCell(cell, text, style) { var div = document.createElement('div'), // create DIV element txt = document.createTextNode(text); // create text node div.appendChild(txt); // append text node to the DIV div.setAttribute('class', style); // set DIV class attribute div.setAttribute('className', style); // set DIV class attribute for IE (?!) cell.appendChild(div); // append DIV to the table cell }
createCell() also sets class attribute (needed for the red and green background color) to the DIV element. It’ not necessary to create DIV element (you can append only text node to the TD element), but this is a good example of how to create and append new element to the table cell. appendColumn() is similar to the appendRow() function. For each table row, new cell is inserted to the last position in the row.
// append column to the HTML table function appendColumn() { var tbl = document.getElementById('my-table'), // table reference i; // open loop for each row and append cell for (i = 0; i < tbl.rows.length; i++) { createCell(tbl.rows[i].insertCell(tbl.rows[i].cells.length), i, 'col'); } }
Here is code used to delete table rows and columns (“Delete both” button, calls both functions).
// delete table rows with index greater then 0 function deleteRows() { var tbl = document.getElementById('my-table'), // table reference lastRow = tbl.rows.length - 1, // set the last row index i; // delete rows with index greater then 0 for (i = lastRow; i > 0; i--) { tbl.deleteRow(i); } } // delete table columns with index greater then 0 function deleteColumns() { var tbl = document.getElementById('my-table'), // table reference lastCol = tbl.rows[0].cells.length - 1, // set the last column index i, j; // delete cells with index greater then 0 (for each row) for (i = 0; i < tbl.rows.length; i++) { for (j = lastCol; j > 0; j--) { tbl.rows[i].deleteCell(j); } } }
This has to be one of the most handy articles I’ve read in a good long while! Thanks!
This is a good article, very useful. Although, I can’t seem to make the delete columns function work, any help?
GREAT ! thx so much for sharing :D
great,
thanx lot..
very usefull.full fill my need.
advance wishes for future articles.
Thank you so much!
You saved me a lot of time.
:)
how can i delete one row in a click?
how can i edit the row and send data to the database?
how can i alter the color of the rows?
Great tips! Thanks…
how can i remove a single row /column
@hyma – JavaScript method deleteRow() – deletes a row from the rows collection. Optional parameter is the row index in the rows collection or if omitted then the last row will be removed. On the other hand, deleting column is a bit complicated because you can not just delete whole column. Instead you have to delete each cell separately with deleteCell() method. In this example I used loops to delete rows and columns at once. You will only have to slightly modify JavaScript functions from the last code part to delete row by row or column by column.
thanks .very inteligetn article.good luck
this is very helpful. thank you soo much for posting.
Very very helpful, thanks a bunch!
Code for deleting rows and columns one by one can be found here:
http://tapas4web.blogspot.com/2011/05/javascript-addremove-rowscolumns-in.html
I was searching for the same requirement. The code posted here is great! Just the requirement is little bit different. So thought everybody should benefit.
Enjoy DOT NET.
Very very useful artical…helped a lot!!!
this is good example for dynamic creation thanks for it…..@@@@
I have similar requirement where I need to Add a Column “i.e. Users details” with check boxes in each row, but after using this code unable to read that column. Could you please help me to undertsand this how it works..
@vivek – Appending column is easy: loop goes through each row and append table cell to the row end. To be more clear, in newly created table cells (I mean column) DIV elements are placed to visually describe new column. Please take a look to the appendColumn() JavaScript source.
really excellent example
the program done by a person is appreciable really. Really good.
Nice code… easy to understand….