-1

What I am trying to do is to save the current tab background color in a var, and later use that var to change the body color. But why is it not working?

const tabName = 'someId';
var thisTabColor = document.getElementById(tabName).style.backgroundColor;
document.body.style.backgroundColor = thisTabColor;
#someId {
  background-color: #cccccc; /* or whatever */
}
<body>
  <div id="someId">Some text here</div>
</body>

In the above example, the body's background doesn't change to #cccccc, the div's color.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Yunhao Lin
  • 91
  • 9

3 Answers3

2

var tabName = 'container';
var tabElement = document.getElementById(tabName);
var computedStyles = window.getComputedStyle(tabElement);
var thisTabColor = computedStyles.getPropertyValue('background-color');
document.body.style.backgroundColor = thisTabColor;
#container {
  background-color: red;
  border: 1px solid black;
}
<div id="container">Text</div>
Pingolin
  • 3,161
  • 6
  • 25
  • 40
0

remove the #

document.body.style.backgroundColor = thisTabColor;
Endless
  • 34,080
  • 13
  • 108
  • 131
0

why you concatenate with "#"? the 1st instruction will return the color value you should remove the #