---
title: "html table insert/delete rows"
author: "jdlsfl"
date: "2009-02-07T20:31:40Z"
tags: []
slug: "html-table-insert-delete-rows"
draft: false
lastmod: "2009-02-07T20:31:40Z"
id: 324992
url: "https://jdlsfl.iteye.com/blog/324992"
---

[Deleting table rows  
&lt;**html**&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
**function** deleteRow(i){  
document.getElementById('myTable').deleteRow(i)  
}  
&lt;/script&gt;  
&lt;/head&gt;  
\
&lt;**body**&gt;  
&lt;table id="myTable" border="1"&gt;  
&lt;tr&gt;  
&lt;td&gt;Row 1&lt;/td&gt;  
&lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"&gt;&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;Row 2&lt;/td&gt;  
&lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"&gt;&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;Row 3&lt;/td&gt;  
&lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"&gt;&lt;/td&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;/**body**&gt;  
\
&lt;/**html**&gt;]()

Adding table rows

&lt;**html**&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
**function** insRow(){  
    var x=document.getElementById('myTable').insertRow(2)  
    var y=x.insertCell(0)  
    var z=x.insertCell(1)  
    y.innerHTML="NEW CELL1"  
    z.innerHTML="NEW CELL2"  
}  
&lt;/script&gt;  
&lt;/head&gt;

&lt;**body**&gt;  
&lt;table id="myTable" border="1"&gt;  
    &lt;tr&gt;  
        &lt;td&gt;d&lt;/td&gt;  
        &lt;td&gt;d&lt;/td&gt;  
    &lt;/tr&gt;  
    &lt;tr&gt;  
        &lt;td&gt;d&lt;/td&gt;  
        &lt;td&gt;d&lt;/td&gt;  
    &lt;/tr&gt;  
    &lt;tr&gt;  
        &lt;td&gt;Row3 cell1&lt;/td&gt;  
        &lt;td&gt;Row3 cell2&lt;/td&gt;  
    &lt;/tr&gt;  
    &lt;tr&gt;  
        &lt;td&gt;Row4 cell1&lt;/td&gt;  
        &lt;td&gt;Row4 cell2&lt;/td&gt;  
    &lt;/tr&gt;  
    &lt;tr&gt;  
        &lt;td&gt;Row5 cell1&lt;/td&gt;  
        &lt;td&gt;Row5 cell2&lt;/td&gt;  
    &lt;/tr&gt;  
&lt;/table&gt;  
&lt;**form**&gt;  
&lt;input type="button" onclick="insRow()" value="Insert row"&gt;  
&lt;/**form**&gt;  
&lt;/**body**&gt;

&lt;/**html**&gt;

Inserting/Removing Row Elements  
&lt;**HTML**&gt;  
&lt;HEAD&gt;  
&lt;TITLE&gt;Modifying Table Cell Content&lt;/TITLE&gt;  
&lt;STYLE TYPE="text/css"&gt;  
THEAD {background-color:lightyellow; font-weight:bold}  
TFOOT {background-color:lightgreen; font-weight:bold}  
#myTABLE {background-color:bisque}  
&lt;/STYLE&gt;  
&lt;**SCRIPT** LANGUAGE="JavaScript"&gt;  
var theTable, theTableBody  
**function** init() {  
    theTable = (document.all) ? document.all.myTABLE :   
        document.getElementById("myTABLE")  
    theTableBody = theTable.tBodies\[0]  
}  
**function** appendRow(**form**) {  
    insertTableRow(form, -1)  
}  
**function** addRow(**form**) {  
    insertTableRow(form, **form**.insertIndex.value)  
}  
**function** insertTableRow(form, where) {  
    var now = **new** Date()  
    var nowData = [now.getHours(), now.getMinutes(), now.getSeconds(),   
        now.getMilliseconds()]  
    clearBGColors()  
    var newCell  
    var newRow = theTableBody.insertRow(where)  
    **for** (var i = 0; i &lt; nowData.length; i++) {  
        newCell = newRow.insertCell(i)  
        newCell.innerHTML = nowData\[i]  
        newCell.style.backgroundColor = "salmon"  
    }  
    updateRowCounters(**form**)  
}  
**function** removeRow(**form**) {  
    theTableBody.deleteRow(**form**.deleteIndex.value)  
    updateRowCounters(**form**)  
}  
**function** insertTHEAD(**form**) {  
    var THEADData = \["Hours","Minutes","Seconds","Milliseconds"]  
    var newCell  
    var newTHEAD = theTable.createTHead()  
    newTHEAD.id = "myTHEAD"  
    var newRow = newTHEAD.insertRow(-1)  
    **for** (var i = 0; i &lt; THEADData.length; i++) {  
        newCell = newRow.insertCell(i)  
        newCell.innerHTML = THEADData\[i]  
    }  
    updateRowCounters(**form**)  
    **form**.addTHEAD.disabled = **true**  
    **form**.deleteTHEAD.disabled = **false**  
}  
**function** removeTHEAD(**form**) {  
    theTable.deleteTHead()      
    updateRowCounters(**form**)  
    **form**.addTHEAD.disabled = **false**  
    **form**.deleteTHEAD.disabled = **true**  
}  
**function** insertTFOOT(**form**) {  
    var TFOOTData = \["Hours","Minutes","Seconds","Milliseconds"]  
    var newCell  
    var newTFOOT = theTable.createTFoot()  
    newTFOOT.id = "myTFOOT"  
    var newRow = newTFOOT.insertRow(-1)  
    **for** (var i = 0; i &lt; TFOOTData.length; i++) {  
        newCell = newRow.insertCell(i)  
        newCell.innerHTML = TFOOTData\[i]  
    }  
    updateRowCounters(**form**)  
    **form**.addTFOOT.disabled = **true**  
    **form**.deleteTFOOT.disabled = **false**  
}

**function** removeTFOOT(**form**) {  
    theTable.deleteTFoot()      
    updateRowCounters(**form**)  
    **form**.addTFOOT.disabled = **false**  
    **form**.deleteTFOOT.disabled = **true**  
}  
**function** insertCaption(**form**) {  
    var captionData = **form**.captionText.value  
    var newCaption = theTable.createCaption()  
    newCaption.innerHTML = captionData  
    **form**.addCaption.disabled = **true**  
    **form**.deleteCaption.disabled = **false**  
}  
**function** removeCaption(**form**) {  
    theTable.deleteCaption()      
    **form**.addCaption.disabled = **false**  
    **form**.deleteCaption.disabled = **true**  
}  
// housekeeping functions  
**function** updateRowCounters(**form**) {  
    var sel1 = **form**.insertIndex  
    var sel2 = **form**.deleteIndex  
    sel1.options.length = 0  
    sel2.options.length = 0  
    **for** (var i = 0; i &lt; theTableBody.rows.length; i++) {  
        sel1.options\[i] = **new** Option(i, i)  
        sel2.options\[i] = **new** Option(i, i)  
    }  
    **form**.removeRowBtn.disabled = (i==0)  
}  
**function** clearBGColors() {  
    **for** (var i = 0; i &lt; theTableBody.rows.length; i++) {  
        **for** (var j = 0; j &lt; theTableBody.rows\[i].cells.length; j++) {  
            theTableBody.rows\[i].cells\[j].style.backgroundColor = ""          
        }  
    }  
}  
&lt;/**SCRIPT**&gt;  
&lt;/HEAD&gt;  
&lt;**BODY** onLoad="init()"&gt;  
&lt;H1&gt;Modifying Tables&lt;/H1&gt;  
&lt;HR&gt;  
&lt;FORM NAME="controls"&gt;  
&lt;FIELDSET&gt;  
&lt;LEGEND&gt;Add/Remove Rows&lt;/LEGEND&gt;  
&lt;TABLE WIDTH="100%" CELLSPACING=20&gt;&lt;TR&gt;  
&lt;TD&gt;&lt;INPUT TYPE="button" VALUE="Append 1 Row"   
    onClick="appendRow(this.form)"&gt;&lt;/TD&gt;  
&lt;TD&gt;&lt;INPUT TYPE="button" VALUE="Insert 1 Row" onClick="addRow(this.form)"&gt; at index:   
    &lt;SELECT NAME="insertIndex"&gt;  
        &lt;OPTION VALUE="0"&gt;0  
    &lt;/SELECT&gt;&lt;/TD&gt;  
&lt;TD&gt;&lt;INPUT TYPE="button" NAME="removeRowBtn" VALUE="Delete 1 Row" DISABLED   
    onClick="removeRow(this.form)"&gt; at index:   
    &lt;SELECT NAME="deleteIndex"&gt;  
        &lt;OPTION VALUE="0"&gt;0  
    &lt;/SELECT&gt;&lt;/TD&gt;  
&lt;/TR&gt;  
&lt;/TABLE&gt;  
&lt;/FIELDSET&gt;  
&lt;FIELDSET&gt;  
&lt;LEGEND&gt;Add/Remove THEAD and TFOOT&lt;/LEGEND&gt;  
&lt;TABLE WIDTH="100%" CELLSPACING=20&gt;&lt;TR&gt;  
&lt;TD&gt;&lt;INPUT TYPE="button" NAME="addTHEAD" VALUE="Insert THEAD"   
    onClick="insertTHEAD(this.form)"&gt;&lt;BR&gt;  
    &lt;INPUT TYPE="button" NAME="deleteTHEAD" VALUE="Remove THEAD" DISABLED   
        onClick="removeTHEAD(this.form)"&gt;  
&lt;/TD&gt;  
&lt;TD&gt;&lt;INPUT TYPE="button" NAME="addTFOOT" VALUE="Insert TFOOT"   
    onClick="insertTFOOT(this.form)"&gt;&lt;BR&gt;  
    &lt;INPUT TYPE="button" NAME="deleteTFOOT" VALUE="Remove TFOOT" DISABLED   
        onClick="removeTFOOT(this.form)"&gt;  
&lt;/TD&gt;  
&lt;/TR&gt;  
&lt;/TABLE&gt;  
&lt;/FIELDSET&gt;  
&lt;FIELDSET&gt;  
&lt;LEGEND&gt;Add/Remove Caption&lt;/LEGEND&gt;  
&lt;TABLE WIDTH="100%" CELLSPACING=20&gt;&lt;TR&gt;  
&lt;TD&gt;&lt;INPUT TYPE="button" NAME="addCaption" VALUE="Add Caption"   
    onClick="insertCaption(this.form)"&gt;&lt;/TD&gt;  
&lt;TD&gt;Text: &lt;INPUT TYPE="text" NAME="captionText" SIZE=40 VALUE="Sample Caption"&gt;  
&lt;TD&gt;&lt;INPUT TYPE="button" NAME="deleteCaption" VALUE="Delete Caption" DISABLED   
    onClick="removeCaption(this.form)"&gt;&lt;/TD&gt;  
&lt;/TR&gt;  
&lt;/TABLE&gt;  
&lt;/FIELDSET&gt;  
&lt;/FORM&gt;  
&lt;HR&gt;  
&lt;TABLE ID="myTABLE" CELLPADDING=10 BORDER=1&gt;  
&lt;TBODY&gt;  
&lt;/TABLE&gt;  
&lt;/**BODY**&gt;  
&lt;/**HTML**&gt;