Visit Mozilla.org

DOM:element.previousSibling

From MDC

« Gecko DOM Reference

Contents

[edit] Summary

Returns the node immediately preceding the specified one in its parent's childNodes list, null if the specified node is the first in that list.

[edit] Syntax

previousNode = node.previousSibling

[edit] Example

// <a><b1 id="b1"/><b2 id="b2"/></a>
alert(document.getElementById("b1").previousSibling); // null
alert(document.getElementById("b2").previousSibling.id); // "b1"

[edit] Notes

Gecko-based browsers insert text nodes into a document to represent whitespace in the source markup. Therefore a node obtained for example via firstChild or previousSibling may refer to a whitespace text node, rather than the actual element the author intended to get.

See Whitespace in the DOM and W3C DOM 3 FAQ: Why are some Text nodes empty? for more information.


To navigate the opposite way through the child nodes list use element.nextSibling.

[edit] Specification

DOM Level 1 Core: previousSibling

DOM Level 2 Core: previousSibling