English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM previousElementSibling property

HTML DOM Element Object

previousElementSiblingA read-only property at the same tree level returns the previous element of the specified element.

If there is no preceding element at the same level, this property returnsnull.

UsenextElementSiblingThis property returns the next element of the specified element.

UsechildrenThis property returns any child elements of the specified element.

Syntax:

element.previousElementSibling
var x = document.querySelector("#div-2.previousElementSibling.innerHTML;
Test and See‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the previousElementSibling property:

Property
previousElementSibling23.51049

Technical Details

Return Value:A Node object representing the previous sibling of an element; if there is no previous sibling, it isnull
DOM Version:DOM Level3

More Examples

Change the HTML content of the previous sibling of the second DIV element:

var div = document.querySelector("#div-2)
div.previousElementSibling.innerHTML = "HELLO WORLD";
Test and See‹/›

Change the background color of the previous sibling of the second DIV element:

var div = document.querySelector("#div-2)
div.previousElementSibling.style.backgroundColor = "coral";
Test and See‹/›

Related References

HTML DOM Reference:children property

HTML DOM Reference:nextElementSibling property

HTML DOM Element Object