0

I've been trying to get a fade in and out for a background image for a site, and I have been trying to get the background color of a div into a variable, this is what I've tried:

 elem = document.getElementById('nav');
 bgColor = elem.style.backgroundColor;

But once I try to alert the variable bgColor like: alert(bgColor) all I get alerted is empty in the text box. I've looked around on some Stack questions and I've tried everything that is told there and it doesn't seem to put the physical color into a variable.

Jack Hales
  • 1,574
  • 23
  • 51
  • [`window.getComputedStyle()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle) – Andreas Mar 24 '16 at 10:56

1 Answers1

0

window.getComputedStyle is the solution. Otherwise you'll get the "direct" styles that are empty.

Alteyss
  • 513
  • 3
  • 17