Web/API/Node/previousSibling

From Get docs
< Web/API‎ | Node


The Node.previousSibling read-only property returns the node immediately preceding the specified one in its parent's childNodes list, or null if the specified node is the first in that list.

Syntax

previousNode = node.previousSibling;

Example

<img id="b0">
<img id="b1">
<img id="b2">
console.log(document.getElementById("b1").previousSibling); // <img id="b0">
console.log(document.getElementById("b2").previousSibling.id); // "b1"

Notes

Gecko-based browsers insert text nodes into a document to represent whitespace in the source markup. Therefore a node obtained, for example, using Node.firstChild or Node.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 Node.nextSibling.

Specifications

Specification Status Comment
DOMThe definition of 'Node.previousSibling' in that specification. Living Standard No change
Document Object Model (DOM) Level 3 Core SpecificationThe definition of 'Node.previousSibling' in that specification. Obsolete No change
Document Object Model (DOM) Level 2 Core SpecificationThe definition of 'Node.previousSibling' in that specification. Obsolete No change
Document Object Model (DOM) Level 1 SpecificationThe definition of 'Node.previousSibling' in that specification. Obsolete Initial definition

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
previousSibling Chrome

Full support Yes

Edge

Full support 12

Firefox

Full support Yes

IE

Full support 5.5

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support Yes

Firefox Android

Full support Yes

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support Yes

Legend

Full support  
Full support


See also