nextSibling - previousSibling Properties

calendar November 24, 2009

Value: Object reference

Compatibility: WinIE5+, MacIE5+, Moz1+, Safari1+

A sibling node is one at the same nested level as another node in the hierarchy of an HTML document. The following p element has tow child nodes (the i and b elements), those tow child nodes are siblings of each other.

<p>Ramones is <i>the</i>  best <b>rock’n'roll</b> band.</p>

The i node has not previousSibling property, the b node has not nextSibling prorerty (return null).

The following example assings the same class name to all child nodes of an element:

function setChildClasses(parentElem, className)
{
  var childElem = parentElem.firstChild;
  while(childElem.nextSibling)
  {
    childElem.className = className;
    childElem = childElem.nextSibling;
  }
}

Related items: firstChild, lastChild, childNodes properties, insertAdjucentElement method.

admin

Leave a Reply

You must be logged in to post a comment.