previousSibling (W3C DOM Core property)

Spec
Version Depr. Static Read-only
DOM1 No No Yes
Browser support (more…)
IE5.5+ FF1.5+ SA1.3+ OP9+
Full Full Full Full

Example

var sibling = node.previousSibling;

In the example above, if node has a sibling immediately before it (a node of any type, which shares its parent with node), the sibling variable will be a reference to that node; otherwise it will be null.

So in the following HTML example, the first li element is the previousSibling of the second, but has no previousSibling of its own:

<h2>Shopping list</h2>
<ul>
  <li>Beer</li>
  <li>More beer</li>
</ul>
Note: This example doesn’t take account of whitespace

Actually this example is idealized, and in some browsers the first li element may not in fact be the previousSibling of the second; that's because some browsers count intermediate whitespace as text nodes, and would therefore consider the previousSibling to be the whitespace between the </li> and <li> tags.

For more about this behavior please see DOM Core.

Description

The node immediately before this node (ie. both nodes have the same parent and are next to each other in its childNodes collection), or null if there is no such node.

This property is readonly.

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
Full Full Full Full Full Full Full Full Full Full Full

No known issues.

User-contributed notes

There are no comments yet.

Related Products