Friday, January 22, 2010

Add multiple rows dynamically with javascript

this function can be modified to take the amount of rows to create. i just hard coded the amount due to time :D

javascript code

function addTableCell(tblElement)
{
var tbl = document.getElementById(tblElement);
var celTitle = "Session Title";
var cellName = "session_title";

for( var n = 0; n < lastrow =" tbl.rows.length;" iteration =" lastRow;" row =" tbl.insertRow(lastRow);" titlecell =" row.insertCell(0);" titlelabel =" document.createTextNode(celTitle);" cellright2 =" row.insertCell(1);" el2 =" document.createElement('input');" type =" 'text';" name =" cellName" size =" '30';" class =" 'flatTextbox';" celtitle = "Session Description" cellname = "session_desc" n =" 0;" tbl =" document.getElementById(tblElement);" tbl =" document.getElementById('addcell');" lastrow =" tbl.rows.length;"> 1) tbl.deleteRow(lastRow - 1);
//if (lastRow > 3) tbl.deleteRow(lastRow - 1);
}

}

html code
this was in another table. highlight and view code for fields and buttons









Session Title
Session Description


and this is the code to accomplish adding and removing the last inserted fields




No comments: