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.
previousNode = node.previousSibling;
<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"
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.
Specification | Status | Comment |
---|---|---|
DOM The definition of 'Node.previousSibling' in that specification. | Living Standard | No change |
Document Object Model (DOM) Level 3 Core Specification The definition of 'Node.previousSibling' in that specification. | Obsolete | No change |
Document Object Model (DOM) Level 2 Core Specification The definition of 'Node.previousSibling' in that specification. | Obsolete | No change |
Document Object Model (DOM) Level 1 Specification The definition of 'Node.previousSibling' in that specification. | Obsolete | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | 12 | ? | ? | Yes | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | ? | ? | Yes | ? | Yes |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Node/previousSibling