I am looking for a way to select the css variable --bgcolor of the element being hovered and assign that value to the body css
Here is my current attempt, any suggestions?
var body = document.querySelector('body');
var boxList = document.querySelectorAll('.box');
var box = Array.prototype.slice.call(boxList);
box.forEach(function (el) {
el.addEventListener('mouseover', ()=> {
var newbg = el.style.background;
body.style.background = newbg;
});
});
body {
background: var(--bgcolor);
}
.box {
width: 80px;
height: 80px;
margin: 10px;
background: var(--bgcolor);
display:inline-block;
}
<body>
<div class="box" style="--bgcolor: #FF0000"></div>
<div class="box" style="--bgcolor: #00FF00"></div>
<div class="box" style="--bgcolor: #0000FF"></div>
</body>