On Fri, 9 Apr 2004 18:25:52 +0200, Ivo <no@thank.you > wrote:
[snip]
Funnily enough, with "this". Namely "this.lastSibli ng" is the first
DOM nodes don't have a lastSibling property. What you're thinking of is
Node.previousSi bling, and possibly getting confused with Node.lastChild.
td (in this case) and "this.nextSibli ng" the last. Even,
In all likelihood, you'll find that Node.previousSi bling and
Node.nextSiblin g refer to text nodes, not elements. If the HTML is
structured with no whitespace (of any kind) between elements, then
elements will be continuous.
"this.parentNod e" is the tr in which all td's all contained. If you use
these DOM expressions there is no need to give each table cell its own
id.
function gointo(td,color ){
if (td.nextSibling ) // if td is the last in a row, there is no next
td.nextSibling. style.backgroun d="#"+color;
// or change whole tr
td.parentNode.s tyle.background ="#"+color;
}
To the OP: use the following functions to find the first, last and "other"
TD elements
/* Searches the given element's siblings for the
first occurance of a specific type of element.
If a matching node cannot be found, null is
returned.
ref - node reference from which the search will begin
type - name of a HTML element (in uppercase)
*/
function getNextElement( ref, type ) {
var node = ref;
while( node && ( type != node.nodeName )) {
node = node.nextSiblin g;
}
return node;
}
/* Works in exactly the same way as getNextElement( ),
except for the direction of the search.
*/
function getPreviousElem ent( ref, type ) {
var node = ref;
while( node && ( type != node.nodeName )) {
node = node.previousSi bling;
}
return null;
}
/* Searches the given element's children for the
first occurance of a specific type of element.
If a matching node cannot be found, null is
returned.
ref - node reference
type - name of a HTML element (in uppercase)
*/
function getFirstElement ( parent, type ) {
var node = null;
if( parent.firstChi ld ) {
node = parent.firstChi ld;
} else if( parent.childNod es ) {
node = parent.childNod es[ 0 ];
}
return getNextElement( node, type );
}
/* Searches the given element's children for the
last occurance of a specific type of element.
If a matching node cannot be found, null is
returned.
ref - node reference
type - name of a HTML element (in uppercase)
*/
function getLastElement( parent, type ) {
var node = null;
if( parent.lastChil d ) {
node = parent.lastChil d;
} else if( parent.childNod es ) {
node = parent.childNod es[ parent.childNod es.length - 1 ];
}
return getPreviousElem ent( node, type );
}
Ivo's gointo() replacement would then look like this:
function gointo( td, colour ) {
var next = getNextElement( td, 'TD' );
if( next && next.style ) {
next.style.back groundColor = colour;
}
}
To set the first TD, you could add:
var first = getFirstElement ( td.parentNode, 'TD' );
if( first && first.style ) {
first.style.bac kgroundColor = colour;
}
and similarly for the last TD.
Hope that helps,
Mike
--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)