You are here: Reference > JavaScript > client-side > HTML DOM > objects > nodes and tags > table

table object

Browser support:
Specifies an element that arranges its contents into rows and columns.
The table element is commonly used by authors as a layout device.
The advantage of using table elements for layouts:
  • Several different arrangements can be implemented with table elements.
  • In most cases, the layout is rendered identically in all commonly used browsers.
Drawbacks:
  • In some cases when the width or height property of a table element is set in percent, the rendered size of the table can be different in browsers. In that case, using absolute or static positioned elements can solve the problem.
Elements that belong to a table:
There are three different cluster elements within a table, the thead (table header), tbody (table body) and the tfoot (table footer) elements that can be used to create a well formatted data table. Like every data table, a HTML table element can contain rows (tr) and cells (th, td).
Other elements that can be used with a table:
  • caption – Inserts a caption to the top or bottom of a table. Every table element may have only one caption element.
  • col, colgroup elements – Specify rules for one or more table columns in the table, but they are not recommended to use, because most browsers support these functionalities only partially.
Table elements in action:
The HTML code of this table is implemented in Example 2.

Syntax:

Methods that return the object:
document.createElement ("table")
The base interface, through which you can add new functionalities to the table object, is the HTMLTableElement interface.
If you want to see the HTML objects by categories, please visit this page.
HTML page for this element: table

Possible members:

Properties
Methods
Events
Style properties
accessKey
Sets or retrieves an access key to an element.
align
Sets or retrieves the horizontal alignment of an object relative to its parent.
all
Represents a collection of all elements contained by an element or the entire document.
attributes
Represents a collection of attribute nodes that belong to an element.
background
Sets or retrieves the background picture, which will be tiled if it is smaller than the object's dimensions.
baseURI
10
Returns the base URL for the object.
behaviorUrns
Represents a collection of the Uniform Resource Names for all behaviors attached to an element.
bgColor
Sets or retrieves the background color.
border
Sets or retrieves the thickness of the border.
borderColor
Sets or retrieves the color of the border.
borderColorDark
Sets or retrieves the color used to draw the right and bottom borders of a table.
borderColorLight
Sets or retrieves the color used to draw the left and top borders of a table.
canHaveChildren
Retrieves a Boolean value that indicates whether the element can contain child elements.
canHaveHTML
Retrieves a Boolean value that indicates whether the element can contain HTML formatted text.
caption
Returns the caption element of the current table object.
cellPadding
Specifies or returns the amount of space between the border of a cell and its contents in a table.
cells
Represents a collection of all td or th elements that belong to a table or tr element.
cellSpacing
Sets or retrieves the amount of space between the border of the cells in a table.
childElementCount
93.5
Returns the number of element nodes that are direct descendants of the current element.
childNodes
Represents a collection of all nodes that are direct descendants of an element.
children
3.5
Represents a collection of all element nodes that are direct descendants of an element.
className
Sets or retrieves the style class or classes that belong to the element.
clientHeight
Returns the height of the visible area for an object, in pixels. The value contains the height with the padding, but it does not include the scrollBar, border, and the margin.
clientLeft
Returns the width of the left border in pixels.
clientTop
Returns the height of the top border in pixels.
clientWidth
Returns the width of the visible area for an object, in pixels. The value contains the width with the padding, but does not include the scrollBar, border, and the margin.
cols
Specifies or returns the number of columns in a table.
contentEditable
3
Sets or retrieves whether the contents of the object are editable.
currentStyle
Represents the computed style settings for an element.
dataFld
Specifies or returns which field of a given data source should be bound to the specified object.
dataFormatAs
Specifies or returns how data is to be rendered.
dataPageSize
Sets or retrieves the number of records that can be displayed in a table when data binding is used.
dataSrc
Sets or retrieves the identifier of the data source that is bound to the element.
dir
Sets or retrieves the text direction as related to the lang property.
disabled
Sets or retrieves the state of an object for user interaction.
draggable
3.55
Sets or retrieves whether an element is draggable.
filters
Represents a collection of all filter objects applied to an element.
firstChild
Returns a reference to the first child of the current element.
firstElementChild
93.5
Returns a reference to the first child element of the current element.
frame
Sets or retrieves which borders of a table element should be displayed.
height
Specifies or returns the height of an element.
hideFocus
Specifies or returns whether a dotted rectangle (focus rectangle) is drawn around an object while it has focus.
hspace
Specifies or returns the number of pixels to use as a margin at the left and right sides of the object.
id
Sets or retrieves a unique identifier for the object.
innerHTML
Sets or retrieves the inner HTML content (the source code between the opening and closing tags) of an element.
innerText
Sets or returns the text content of an element including the text content of its descendants.
isContentEditable
Returns a Boolean value that indicates whether the contents of the object are editable by the user.
isDisabled
Returns a Boolean value that indicates whether the object is disabled.
isMultiLine
Returns a Boolean value that indicates whether the contents of an element can be multiline or not.
isTextEdit
Returns a Boolean value that indicates whether the createTextRange method can be used for the element.
lang
Specifies or returns the language of the element.
language
Sets or retrieves the scripting language for the current element. Use it only for the script element.
lastChild
Returns a reference to the last child of the current element.
lastElementChild
93.5
Returns a reference to the last child element of the current element.
localName
9
Returns the local part of the qualified name of the current node.
name
Sets or retrieves the name of an element.
namespaceURI
93.6
Sets or returns the namespace URI of the current node.
nextElementSibling
93.5
Returns a reference to the next child element of the current element's parent.
nextSibling
Returns a reference to the next child of the current element's parent.
nodeName
Returns the name of the current node.
nodeType
Returns an integer that indicates the type of the node.
nodeValue
Sets or returns the value of the current node.
offsetHeight
Returns the height of the visible area for an object, in pixels. The value contains the height with the padding, scrollBar, and the border, but does not include the margin.
offsetLeft
Returns the left position of an object relative to the left side of its offsetParent element, in pixels.
offsetParent
Returns a reference to the closest ancestor element in the DOM hierarchy from which the position of the current element is calculated.
offsetTop
Returns the top position of the object relative to the top side of its offsetParent element, in pixels.
offsetWidth
Returns the width of the visible area for an object, in pixels. The value contains the width with the padding, scrollBar, and the border, but does not include the margin.
outerHTML
Sets or retrieves the outer HTML content (the source code including the opening and closing tags) of an element.
outerText
Sets or returns the text content of an element including the text content of its descendants.
ownerDocument
Returns the document object that contains the current node.
parentElement
Returns the parent element of the object in the DOM hierarchy.
parentNode
Returns the parent element of the current node in the DOM hierarchy.
parentTextEdit
Returns the closest ancestor element of the current element in the DOM hierarchy that can be used to create a TextRange object.
previousElementSibling
93.5
Returns a reference to the previous child element of the current element's parent.
previousSibling
Returns a reference to the previous node of the current element's parent.
readyState
Returns a string value that represents the state of the object.
rows
Represents a collection of tr elements in a table, tfoot, thead or tbody element.
rules
Specifies or returns which borders will appear between cells within a table.
runtimeStyle
Represents the overridden style settings for an element.
scopeName
Retrieves the local name of the namespace declared for the current element.
sourceIndex
Returns the position of the current object in the all collection of the document.
spellcheck
Sets or retrieves whether the automatic spellchecker is enabled.
style
Represents the inline style settings for an element or a CSS rule.
summary
Specifies or returns a summary of the data represented in a table.
tabIndex
Specifies or returns the tabbing order for keyboard navigation using the TAB key.
tagName
Returns the tag name of the current element.
tagUrn
Sets or retrieves the Uniform Resource Name (URN) of the namespace declared for the current element.
tBodies
Represents a collection of tbody elements in a table element.
textContent
9
Sets or returns the text content of an element including the text content of its descendants.
tFoot
Returns a reference to the tfoot element of the table.
tHead
Returns a reference to the thead element of the table.
title
Specifies or returns a tooltip for an element.
uniqueID
Returns the unique identifier generated by the browser for the object.
unselectable
Sets or retrieves whether the selection process can start in an element's content.
vspace
Specifies or returns the number of pixels to use as a margin at the top and bottom sides of an object.
width
Specifies or returns the default width of the element.

Example HTML code 1:

This example illustrates the use of the table element:
<table border="1px">
    <caption align="right">Fruits</caption>
    <tr>
        <td>Apple</td>
        <td>Pear</td>
        <td>Peach</td>
    </tr>
</table>
Did you find this example helpful? yes no

Example HTML code 2:

Table with caption, header and footer:
<table cellpadding="4px" cellspacing="2px" border="2px" bordercolor="black">
    <caption style="background-color:#DDDDDD;">Fruits</caption>
    <thead>
        <tr>
            <th>name</th>
            <th>price</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>apple</td>
            <td>3.35$</td>
        </tr>
        <tr>
            <td>peach</td>
            <td>2.21$</td>
        </tr>
    </tbody>
    <tfoot>
        <tr style="font-weight:bold;">
            <td>Sum</td>
            <td>5.56$</td>
        </tr>
    </tfoot>
</table>
Did you find this example helpful? yes no

Example HTML code 3:

This is a more complex example:
<table border="1px" cellpadding="5px" cellspacing="3px" style="background-color:#F9E9E9;">
    <caption align="center">Fruits</caption>
    <thead>
        <tr>
            <th>fruit</th>
            <th>location</th>
            <th>cost</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td rowspan="2">Apple</td>
            <td>Alabama</td>
            <td>12.35</td>
        </tr>
        <tr>
            <td>Arkansas</td>
            <td>8.42</td>
        </tr>
    </tbody>
        <tr>
            <td>Peach</td>
            <td>Kansas</td>
            <td>17.89</td>
        </tr>
</table>
Did you find this example helpful? yes no

Example HTML code 4:

This example creates a table dynamically in JavaScript:
<head>
    <script type="text/javascript">
        function CreateTable () {
            var table = document.createElement ("table");
            table.border = "2px";
            table.cellSpacing = "5px";

            var tHead = table.createTHead ();
            var row = tHead.insertRow (-1);
            for (var i = 0; i < 3; i++) {
                var cell = row.insertCell (-1);
                cell.innerHTML = "Head" + (i+1);
            }

            var tBody = document.createElement ("tbody");
            table.appendChild (tBody);
            for (var j = 0; j < 4; j++) {
                var row = tBody.insertRow (-1);
                for (var i = 0; i < 3; i++) {
                    var cell = row.insertCell (-1);
                    cell.innerHTML = "[" + (j+1) + ", " + (i+1) + "]";
                }
            }

            var tFoot = table.createTFoot ();
            var row = tFoot.insertRow (-1);
            for (var i = 0; i < 3; i++) {
                var cell = row.insertCell (-1);
                cell.innerHTML = "Foot" + (i+1);
            }

            var container = document.getElementById ("container");
            container.appendChild (table);
        }

    </script>
</head>
<body>
    <button onclick="CreateTable ()">Create a new table</button>
    <br /><br />
    <div id="container"></div>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content