MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla

Revision 59731 of Traversing an HTML table with JavaScript and DOM Interfaces

  • Revision slug: Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces
  • Revision title: Traversing an HTML table with JavaScript and DOM Interfaces
  • Revision id: 59731
  • Created:
  • Creator: Dria
  • Is current revision? No
  • Comment

Revision Content

Introduction

This article is an overview of some powerful, fundamental DOM level 1.0 methods and how to use them from JavaScript. You will learn how to create, access and control, and remove HTML elements dynamically. The DOM methods presented here are not specific to HTML; they also apply to XML. The demonstrations provided here will work fine in any browser with full support for DOM level 1, such as the Mozilla browser or others based on it like the next-generation Navigator browser from Netscape. The code samples in this document also work on IE5.

The DOM methods presented here are part of the Document Object Model (Core) level 1 specification. DOM level 1 includes both methods for generic document access and manipulation (DOM 1 Core) as well as methods specific to HTML documents (DOM 1 HTML).

Overview of Sample1.html

This article is an introduction to the DOM through sample code. To get started, try out the following HTML sample. It uses DOM level 1 methods from JavaScript to create an HTML table dynamically. It creates a small table with four cells and text content inside each cell. The text content of the cell is: "cell is row y column x" showing the row and column numbers for that cell in the table.

<head>
<title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script>
    function start() {
        // get the reference for the body
        var mybody=document.getElementsByTagName("body").item(0);
        // creates an element whose tag name is TABLE
        mytable = document.createElement("TABLE");
        // creates an element whose tag name is TBODY
        mytablebody = document.createElement("TBODY");
        // creating all cells
        for(j=0;j<2;j++) {
            // creates an element whose tag name is TR
            mycurrent_row=document.createElement("TR");
            for(i=0;i<2;i++) {
                // creates an element whose tag name is TD
                mycurrent_cell=document.createElement("TD");
                // creates a Text Node
                currenttext=document.createTextNode("cell is row "+j+", column "+i);
                // appends the Text Node we created into the cell TD
                mycurrent_cell.appendChild(currenttext);
                // appends the cell TD into the row TR
                mycurrent_row.appendChild(mycurrent_cell);
            }
            // appends the row TR into TBODY
            mytablebody.appendChild(mycurrent_row);
        }
        // appends TBODY into TABLE
        mytable.appendChild(mytablebody);
        // appends TABLE into BODY
        mybody.appendChild(mytable);
        // sets the border attribute of mytable to 2;
        mytable.setAttribute("border","2");
    }
</script>
</head>
<body onload="start()">
</body>
</html>

Note the order in which we created the elements and the text node:

  1. First we created the TABLE element.
  2. Next, we created the TBODY element, which is a child of the TABLE element.
  3. Next, we used a loop to create the TR elements, which are children of the TBODY element.
  4. For each TR element, we used a loop to create the TD elements, which are children of TR elements.
  5. For each TD element, we then created the text node with the table cell's text.

Once we have created the TABLE, TBODY, TR, and TD elements and then the text node, we then append each object to its parent in the opposite order:

Once we have created the TABLE, TBODY, TR, and TD elements and then the text node, we then append each object to its parent in the opposite order:

  1. First, we attach each text node to its parent TD element using
    mycurrent_cell.appendChild(currenttext);
  2. Next, we attach each TD element to its parent TR element using
    mycurrent_row.appendChild(mycurrent_cell);
  3. Next, we attach each TR element to the parent TBODY element using
    mytablebody.appendChild(mycurrent_row);
  4. Next, we attach the TBODY element to its parent TABLE element using
    mytable.appendChild(mytablebody);
  5. Next, we attach the TABLE element to its parent BODY element using
    mybody.appendChild(mytable);

Remember this technique. You will use it frequently in programming for the W3C DOM. First, you create elements from the top down; then you attach the children to the parents from the bottom up.

Here's the HTML markup generated by the JavaScript code:

...
<TABLE border=5>
<tr><td>cell is row 0 column 0</td><td>cell is row 0 column 1</td></tr>
<tr><td>cell is row 1 column 0</td><td>cell is row 1 column 1</td></tr>
</TABLE>
...

Here's the DOM object tree generated by the code for the TABLE element and its child elements:

Image:sample1-tabledom.jpg

You can build this table and its internal child elements by using just a few DOM methods. Remember to keep in mind the tree model for the structures you are planning to create; this will make it easier to write the necessary code. In the TABLE tree of Figure 1 the element TABLE has one child, the element TBODY. TBODY has two children. Each TBODY's child (TR) has one child (TD). Finally, each TD has one child, a text node.

Revision Source

<h3 name="Introduction"> Introduction </h3>
<p>This article is an overview of some powerful, fundamental DOM level 1.0 methods and how to use them from JavaScript. You will learn how to create, access and control, and remove HTML elements dynamically. The DOM methods presented here are not specific to HTML; they also apply to XML. The demonstrations provided here will work fine in any browser with full support for DOM level 1, such as the Mozilla browser or others based on it like the next-generation Navigator browser from Netscape. The code samples in this document also work on IE5.
</p>
<div class="note">The DOM methods presented here are part of the Document Object Model (Core) level 1 specification. DOM level 1 includes both methods for generic document access and manipulation (DOM 1 Core) as well as methods specific to HTML documents (DOM 1 HTML).</div>
<h3 name="Overview_of_Sample1.html"> Overview of Sample1.html </h3>
<p>This article is an introduction to the DOM through sample code. To get started, try out the following HTML sample. It uses DOM level 1 methods from JavaScript to create an HTML table dynamically. It creates a small table with four cells and text content inside each cell. The text content of the cell is: "cell is row y column x" showing the row and column numbers for that cell in the table.
</p>
<pre>&lt;head&gt;
&lt;title&gt;Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces&lt;/title&gt;
&lt;script&gt;
    function start() {
        // get the reference for the body
        var mybody=document.getElementsByTagName("body").item(0);
        // creates an element whose tag name is TABLE
        mytable = document.createElement("TABLE");
        // creates an element whose tag name is TBODY
        mytablebody = document.createElement("TBODY");
        // creating all cells
        for(j=0;j&lt;2;j++) {
            // creates an element whose tag name is TR
            mycurrent_row=document.createElement("TR");
            for(i=0;i&lt;2;i++) {
                // creates an element whose tag name is TD
                mycurrent_cell=document.createElement("TD");
                // creates a Text Node
                currenttext=document.createTextNode("cell is row "+j+", column "+i);
                // appends the Text Node we created into the cell TD
                mycurrent_cell.appendChild(currenttext);
                // appends the cell TD into the row TR
                mycurrent_row.appendChild(mycurrent_cell);
            }
            // appends the row TR into TBODY
            mytablebody.appendChild(mycurrent_row);
        }
        // appends TBODY into TABLE
        mytable.appendChild(mytablebody);
        // appends TABLE into BODY
        mybody.appendChild(mytable);
        // sets the border attribute of mytable to 2;
        mytable.setAttribute("border","2");
    }
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="start()"&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Note the order in which we created the elements and the text node:
</p>
<ol><li> First we created the TABLE element.
</li><li> Next, we created the TBODY element, which is a child of the TABLE element.
</li><li> Next, we used a loop to create the TR elements, which are children of the TBODY element.
</li><li> For each TR element, we used a loop to create the TD elements, which are children of TR elements.
</li><li> For each TD element, we then created the text node with the table cell's text.
</li></ol>
<p>Once we have created the TABLE, TBODY, TR, and TD elements and then the text node, we then append each object to its parent in the opposite order:
</p><p>Once we have created the TABLE, TBODY, TR, and TD elements and then the text node, we then append each object to its parent in the opposite order:
</p>
<ol>
<li>First, we attach each text node to its parent TD element using
<pre>mycurrent_cell.appendChild(currenttext);</pre></li>
<li>Next, we attach each TD element to its parent TR element using
<pre>mycurrent_row.appendChild(mycurrent_cell);</pre></li>
<li>Next, we attach each TR element to the parent TBODY element using
<pre>mytablebody.appendChild(mycurrent_row);</pre></li>
<li>Next, we attach the TBODY element to its parent TABLE element using
<pre>mytable.appendChild(mytablebody);</pre></li>
<li>Next, we attach the TABLE element to its parent BODY element using
<pre>mybody.appendChild(mytable);</pre></li>
</ol>
<p>Remember this technique. You will use it frequently in programming for the W3C DOM. First, you create elements from the top down; then you attach the children to the parents from the bottom up.
</p><p>Here's the HTML markup generated by the JavaScript code:
</p>
<pre>...
&lt;TABLE border=5&gt;
&lt;tr&gt;&lt;td&gt;cell is row 0 column 0&lt;/td&gt;&lt;td&gt;cell is row 0 column 1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;cell is row 1 column 0&lt;/td&gt;&lt;td&gt;cell is row 1 column 1&lt;/td&gt;&lt;/tr&gt;
&lt;/TABLE&gt;
...
</pre>
<p>Here's the DOM object tree generated by the code for the TABLE element and its child elements:
</p><p><img alt="Image:sample1-tabledom.jpg" src="File:en/Media_Gallery/Sample1-tabledom.jpg">
</p><p>You can build this table and its internal child elements by using just a few DOM methods. Remember to keep in mind the tree model for the structures you are planning to create; this will make it easier to write the necessary code. In the TABLE tree of Figure 1 the element TABLE has one child, the element TBODY. TBODY has two children. Each TBODY's child (TR) has one child (TD). Finally, each TD has one child, a text node.
</p>
Revert to this revision