Insert table row: the uniqueID Property : Table « HTML « JavaScript DHTML






Insert table row: the uniqueID Property

 

/*
JavaScript Bible, Fourth Edition
by Danny Goodman 

John Wiley & Sons CopyRight 2001
*/

<HTML>
<HEAD>
<TITLE>Inserting an IE5+/Windows Table Row</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function addRow(item1) {
    if (item1) {
        // assign long reference to shorter var name
        var theTable = document.all.myTable
        // append new row to the end of the table
        var newRow = theTable.insertRow(theTable.rows.length)
        // give the row its own ID
        newRow.id = newRow.uniqueID
        
        // declare cell variable
        var newCell
        
        // an inserted row has no cells, so insert the cells
        newCell = newRow.insertCell(0)
        // give this cell its own id
        newCell.id = newCell.uniqueID
        // display the row's id as the cell text
        newCell.innerText = newRow.id
        newCell.bgColor = "yellow"
        // reuse cell var for second cell insertion
        newCell = newRow.insertCell(1)
        newCell.id = newCell.uniqueID
        newCell.innerText = item1
    }
}
</SCRIPT>
</HEAD>
<BODY>
<TABLE ID="myTable" BORDER=1>
<TR>
<TH>Row ID</TH>
<TH>Data</TH>
</TR>
<TR ID="firstDataRow">
<TD>firstDataRow
<TD>Fred
</TR>
<TR ID="secondDataRow">
<TD>secondDataRow
<TD>Jane
</TR>
</TABLE>
<HR>
<FORM>
Enter text to be added to the table:<BR>
<INPUT TYPE="text" NAME="input" SIZE=25><BR>
<INPUT TYPE='button' VALUE='Insert Row' onClick='addRow(this.form.input.value)'>
</FORM>
</BODY>
</HTML>


           
         
  








Related examples in the same category

1.Tabular data in Javascript with hyper link
2.Change the width of a table border
3.Change the cellPadding and cellSpacing of a table
4.Specify frames of a table
5.Change table row height
6.Specify rules for a table
7.Create table caption
8.Deleting table rows
9.Adding table rows
10.Align the cell content in a table row
11.Change the cell content in a table row
12.Vertical align the cell content in a table row
13.Align the cell content in a single cell
14.Vertical align the cell content in a single cell
15.Adding cells to a table row
16.Change the colspan of a table row
17.Using the cloneNode Method
18.Cycling Through Table frame Property Values
19.Replacing Table Cell Content
20.Inserting/Removing Row Elements
21.Modifying Table Columns
22.Accessing userProfile Data
23.Cycling Through Table rows Property Values
24.Using the Data Binding record Number Property
25.Using the offsetParent Property
26.Transforming JavaScript Data into HTML Tables
27.Transforming JavaScript Data into HTML Tables with HyperLink
28.Create a table
29.Change table border width and cell padding