lundi 23 novembre 2015

if background equals issue

I'm trying to apply an if else statement to an element determining whether another element has a particular background color.

I have stated the element background color as blue in the CSS, however when I add the javascript statement, the element ignores the background color stated in the CSS (blue) and adopts the one stated in the JS IF statement (red).

HTML:

<div id="img"></div>
<div id="color">test</div>

CSS;

#img {
    background:blue;
}

JS

if (document.getElementById('img').style.background = "red")
    {document.getElementById('color').style.background = "green";} 
else {document.getElementById('color').style.background = "red";}

Does anyone know why this is happening?

JS Fiddle example

Aucun commentaire:

Enregistrer un commentaire