0

Please check out code below. This is simple div element with black background. So I'm trying to get background value via JavaScript. But get empty string.

HTML:

<div id="box1"></div>

CSS:

    #box1 {
        width: 20px;
        height: 20px;
        background: #000;
    }

JS:

    var box1 = document.getElementById('box1');

    console.log('backgroundColor: ' + box1.style.backgroundColor);
    console.log('background: ' + box1.style.background);
    console.log('width: ' + box1.style.width);

    setTimeout(function(){
        console.log('backgroundColor: ' + box1.style.backgroundColor);
        console.log('background: ' + box1.style.background);
        console.log('width: ' + box1.style.width);
    }, 1000);

I'm shocked, but result is empty string:

Screenshots:

Chrome

Mozilla

jsfiddle.net/kcg96gdc/

zosuyucot
  • 1
  • 3

0 Answers0