JavaScript Element previousElementSibling Property
Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance property
Overview
What You’ll Learn
Element.previousElementSibling is a read-only instance property that returns the element immediately before the current one in the parent’s children list, or null when there is no previous element. Learn how it differs from previousSibling, how to chain backward traversal, and how to use it safely—with five examples and try-it labs.
01
Kind
Instance property
02
Access
Read-only
03
Type
Element or null
04
Skips
Text & comments
05
Status
Baseline widely
06
Pairs with
nextElementSibling
Fundamentals
Introduction
When you already have one element and need the tag just before it, previousElementSibling is the direct read.
It is perfect for stepping backward across cards, list items, rows, or menu entries without getting tripped up by whitespace text nodes that previousSibling may return.
JavaScript
const third = document.getElementById("div-03");
console.log(third.previousElementSibling.textContent);
💡
Beginner tip
Need the previous node of any type, including text? Use previousSibling. Need only the previous tag? Use previousElementSibling.
Concept
Understanding the Property
MDN: the read-only previousElementSibling property returns the Element immediately prior to the specified one in its parent’s children list, or null if the specified element is the first one in the list.
Read-only — use it to navigate, not assign.
Element or null — never returns text or comment nodes.
Sibling-only — stays on the same parent level.
Baseline — widely available since July 2015 (MDN).
Foundation
📝 Syntax
JavaScript
previousElementSibling
Value
An Element reference for the previous sibling element, or null when there is no preceding element sibling.
Item
Detail
Type
Element or null
Access
Read-only property
Related
previousSibling, nextElementSibling, children
Scope
Only among siblings with the same parent
⚠️
Null safety
If the current element is the first one, previousElementSibling is null. Check before reading properties like tagName or textContent.
Pattern
📋 MDN Backward Loop Shape
MDN starts from one element and walks backward through preceding element siblings:
JavaScript
let el = document.getElementById("div-03").previousElementSibling;
console.log("Siblings of div-03:");
while (el) {
console.log(el.nodeName);
el = el.previousElementSibling;
}
Related learning: previousSibling, nextElementSibling, and children.
Once the current element is already the first element sibling, previousElementSibling becomes null.
Example 4 — Chain Through Siblings Backward
Walk backward across element siblings the same way MDN demonstrates.
JavaScript
let el = document.getElementById("div-03").previousElementSibling;
const names = [];
while (el) {
names.push(el.nodeName);
el = el.previousElementSibling;
}
console.log(names.join(", "));
Each read moves one sibling backward. Repeating it in a loop is a simple way to walk the preceding element siblings.
Example 5 — Support Snapshot
Feature-detect and remember the sibling-only tip.
JavaScript
console.log({
supported: "previousElementSibling" in Element.prototype,
returns: "Element or null",
tip: "Use previousSibling only when you need text/comment nodes too",
status: "Baseline Widely available (MDN)"
});
{
"supported": true,
"returns": "Element or null",
"tip": "Use previousSibling only when you need text/comment nodes too",
"status": "Baseline Widely available (MDN)"
}
How It Works
Safe to use widely. It is the cleaner navigation choice when you only care about element siblings.
Applications
🚀 Common Use Cases
Moving to the previous tab, card, row, or menu item element.
Skipping whitespace nodes that previousSibling would return.
Walking backward through preceding siblings in a loop.
Checking whether an element has a preceding sibling before enabling a "back" action.
Pairing with nextElementSibling for bidirectional sibling navigation.
Under the Hood
🔧 How It Works
1
Current element sits in a sibling list
All children share the same parent.
DOM
2
Browser looks backward
Skips non-element nodes between siblings.
Filter
3
Returns previous Element or null
Null means there is no preceding element sibling.
Read
4
✓
Repeat to keep moving backward
Chaining the property walks across the remaining preceding sibling elements.
Important
📝 Notes
Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
Returns null, not undefined, when no previous element sibling exists.
From the NonDocumentTypeChildNode mixin, so it is available on element, character data, and document type nodes where applicable.
Element.previousElementSibling is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.previousElementSibling
Read-only — returns the preceding sibling Element or null (skips text/comment nodes).
BaselineWidely available
Google ChromeSupported
Yes
Microsoft EdgeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported
Yes
previousElementSiblingBaseline
Bottom line: Use previousElementSibling to move backward across element siblings. Prefer it over previousSibling when whitespace text nodes would only get in the way.
Wrap Up
Conclusion
previousElementSibling is the element-only shortcut to the sibling immediately before the current one. It skips text and comments, returns null at the start, and is perfect for backward sibling navigation.
Use it when you actually need text/comment nodes too
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about previousElementSibling
Read-only Element or null—previous tag sibling only.
5
Core concepts
📄01
Get only
on Element
Kind
📝02
Element|null
previous sibling
Type
🔍03
Backward step
same parent
Use
✅04
Baseline
widely available
Status
🎯05
Skip text
use previousSibling otherwise
Tip
❓ Frequently Asked Questions
It returns the element immediately before the current one in the parent element's children list, or null if there is no preceding element sibling.
No. MDN marks Element.previousElementSibling as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
previousSibling returns the previous node of any type, including text and comment nodes. previousElementSibling skips those and returns only the previous Element.
It returns null when the current element is already the first element sibling.
Yes. You can walk backward across element siblings by reading el.previousElementSibling repeatedly in a loop or step by step.
Yes. It is a read-only property used for navigation, not for assigning a different sibling.
Did you know?
MDN’s looping pattern with el = el.previousElementSibling is one of the simplest ways to walk backward through a sibling row without touching child nodes.