Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Element.previousElementSibling — previous sibling element

The previousElementSibling property returns the nearest earlier element with the same parent, or null, skipping text and comments.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

element.previousElementSibling

Element.previousElementSibling is a read-only property, not a method. It returns the nearest earlier element sharing the same parent, or null when none exists, including when the element being queried has no parent.

Unlike previousSibling, it skips text nodes and comments. It does not search ancestors or descendants. Check that the result is not null before using it as an element.

This is a DOM-standard API, not part of ECMAScript itself. It is defined by the NonDocumentTypeChildNode mixin, included by Element and CharacterData, but not DocumentType. The example requires a browser DOM environment; the repository's Node.js version does not establish availability of this API.

Examples

const parent = document.createElement('div');
const first = document.createElement('span');
const second = document.createElement('button');
parent.append(first, document.createTextNode(' '), document.createComment('gap'), second);

console.log(second.previousElementSibling === first);
console.log(second.previousSibling.nodeType === Node.COMMENT_NODE);
console.log(first.previousElementSibling === null);

Result

true
true
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources