Node.previousSibling

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

La propiedad de sólo-lectura Node.previousSibling devuelve el nodo inmediatamente anterior al especificado en la lista de nodos childNodes de su padre, o null si el nodo especificado es el primero en dicha lista.

Sintaxis

nodoAnterior = nodo.previousSibling;

Ejemplo

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

Notas

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.

Para navegar en el sentido opuesto de la lista de nodos hijos se utiliza Node.nextSibling.

Especificaciones

Specification
DOM
# ref-for-dom-node-previoussibling①

Compatibilidad con navegadores

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
previousSibling

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Ver también