GetPreviousStructuredSibling( objNode )

Downloadable Files

getpreviousstructuredsibling_function1.htm ( 3,804 Bytes )

GetPreviousStructuredSibling() method traverses the sibling chain in the document object model (DOM) searching for the closest previous sibling that is a structured node (ie. not a text node; node of type 3). This is an important function for use in cross-browser scripting where different browsers may or may not acknowledge the existence of text nodes in between structured elements (that are not necessarily expected, such as text nodes between a close and open TR's in a table).

  • function GetPreviousStructuredSibling( objNode ){
  •  
  • // Travel up the sibling chain looking for a non-text node.
  • for (
  • objNode = objNode.previousSibling ;
  • (
  • objNode &&
  • objNode.nodeType == 3
  • ) ;
  • objNode = objNode.previousSibling
  • ){
  • // Nothing has to happen here. The FOR loop itself is taking care
  • // of the node traversing. We don't have to really worry about any
  • // error checking as we can't really make it outside of HTML DOM
  • // elements without hitting a structured node, or coming up with NULL.
  • }
  •  
  • // Return the sibling node.
  • return( objNode );
  •  
  • }

Added May 3, 2006 / Updated May 3, 2006