Add Or Delete Dynamic Rows Using javascript


This Is The Script Do Add Or Delete a row in a table  dynamic. It is Working for me.

<script>

function addRow(tableID)

{

var table = document.getElementById(tableID);

var rowCount = table.rows.length;

if(rowCount<=5) {

var row = table.insertRow(rowCount);

var colCount = table.rows[0].cells.length;

for(var i=0; i<5;i++) {

var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;

switch(newcell.childNodes[0].type) {
case “text”:
newcell.childNodes[0].value = “”;
break;
case “checkbox”:
newcell.childNodes[0].checked = false;
newcell.childNodes[0].style.display=’block’;
break;

}

}

}

if(rowCount>5)
{
alert(‘You Add More Rows ‘);
}

}

function deleteRow(tableID) {

try {

var table = document.getElementById(tableID);

var rowCount = table.rows.length;

for(var i=1; i<= 1;i++) {

var row = table.rows[i];
var chkbox = row.cells[2].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {

alert(“Cannot delete all the rows.”);

break;

}

table.deleteRow(i);

rowCount–;

i–;

}

}

}

catch(e) {

alert(e);

}

}

</script>

Do it In Your Code

var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s