English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
element.previousElementSibling
var x = document.querySelector("#div-2.previousElementSibling.innerHTML;Test and See‹/›
The numbers in the table specify the first browser version that fully supports the previousElementSibling property:
Property | |||||
previousElementSibling | 2 | 3.5 | 10 | 4 | 9 |
Return Value: | A Node object representing the previous sibling of an element; if there is no previous sibling, it isnull |
---|---|
DOM Version: | DOM Level3 |
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‹/›
HTML DOM Reference:children property
HTML DOM Reference:nextElementSibling property