Creating a Scrolling Table Using appendChild() and insertBefore()
for IE5.5+, Netscape 7+, Mozilla 1.1+, and Safari
appendChild() and insertBefore() are methods that let you place nodes in the document. They can be used to place newly created objects and they can be used with nodes already in the document. When used with nodes already in the document, they will first remove the node from it's original location before placing it in it's new location. This tutorial exploits this behaviour to create a scrolling table.
appendChild() uses the syntax destinationNode.appendChild(node) and makes the node the last child node of destinationNode.
insertBefore() uses the syntax destinationNode.insertBefore(node, referenceNode) and places the node before the referenceNode, which is a child node of destinationNode. If you leave out referenceNode the method will behave like appendChild().
Using both methods we can create a table that scrolls up or down. Mouse over 'Up' or 'Down' ( below ) to see it in action.
|
Up
Down
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Here's how it works:
The table id="pa_reps" contains caption, thead, and tbody tags. It's the rows in the tbody that scroll.
It is very important that you remove all the whitespace between the tags in the tbody or the scripts won't work with Netscape.
Table id="pa_reps"PENNSYLVANIA REPRESENTATIVES - 107th CONGRESS1Robert A. Brady Philadelphia // -- the rest of the table edited out to save space
The 'Up' and 'Down' divs use onmouseover and onmouseout to start and stop the scrolling.
<script> // -- get the first tbody of table id="pa_reps" var tb = document.getElementById('pa_reps').getElementsByTagName('tbody')[0]; // -- this will store the ID returned by setInterval() var xx; function startScroll(dir){ if (dir == 'U') xx = setInterval("scrollUp()", 300); else xx = setInterval("scrollDown()", 300); } function scrollUp(){ // -- get the tbody's first child (tr) var r1 = tb.childNodes[0]; if (r1) tb.appendChild(r1); } function scrollDown(){ var r1 = tb.childNodes[0]; // -- get the tbody's last child (tr) var rLast = tb.childNodes[tb.childNodes.length - 1]; if (rLast) tb.insertBefore(rLast, r1); } function stopScroll(){ if (xx != null) clearInterval(xx); } </script>
Netscape 6 Note:
The reason the scrolling table is not recommended with N6 is that N6 has a problem with insertBefore() when you use it to insert the new row before the first row. It seems to put it as the first node of the table tag. It will work fine if you set the referenceNode to anything other than childNode[0]. The only problem is that the rows above referenceNode will never move.
*WARNING!
Netscape 6+ treats whitespace as text nodes.
In the table above, the <tbody> has 22 childNodes (<tr>s). But Netscape 6, because it was counting whitespace, kept insisting that there were 49 childNodes.
In order for this example to work properly in N6 I had to remove ALL the whitespace between the tags in the tbody. Do a 'View Source' to see what I mean.
PXL8 2003