You can access the parentElement, and then get the color from the style.
eg.
const b = document.querySelector("#behindthis");
console.log(b.parentElement.style.backgroundColor);
<div style="background-color: #2eaa69; padding: 40px; box-sinzg: border-box width: 100%;">
<div id="behindthis" style="background-color: #223344; padding: 20px; color: #fff; text-align: center">
get color behind this block
</div>
</div>
One thing to take note, if your style is from CSS you can also use getComputedStyle
.
update: just noticed the div within div, etc. To get the background color here, you could traverse up the parents while the color is transparent. So I've updated the answer to have a bkColor function.
const b = document.querySelector(".this-box");
function bkColor(b) {
const cc = window.getComputedStyle(b);
if (cc.backgroundColor === `rgba(0, 0, 0, 0)`) {
if (b.parentElement) return bkColor(b.parentElement);
}
return cc.backgroundColor;
}
console.log(bkColor(b.parentElement));
.parent-box {
background-color: #2eaa69;
padding: 40px;
}
.this-box {
background-color: #223344;
padding: 20px;
color: #fff;
text-align: center;
}
<div class="parent-box">
<div>
<div class="this-box">
get color behind this block
</div>
</div>
</div>