1

I am trying to create a color mixer where when you click on the first two divs, they change colors. The combination of the colors of the first two divs are supposed to make the third div a mixture of the first two. So if the first two divs are red then the third should also be red. It is not working and the console is saying Uncaught TypeError: Cannot read property 'backgroundColor' of undefined.

HTML

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>

        div {
            width: 100px;
            height: 100px;
            background-color: #00DD00;
            margin: 5px;
            float: left;
        }

    </style>
</head>
<body>
    <script src="js/main.js"></script>
</body>
</html>

Here is my JS

for(var i = 0; i < 1; i++) {

    var newDiv = document.createElement("div");
    newDiv.addEventListener('click', changeColor);
    document.body.appendChild(newDiv);

    var newDivTwo = document.createElement("div");
    newDivTwo.addEventListener('click', changeColor);
    document.body.appendChild(newDivTwo);

    var newDivThree = document.createElement("div");
    newDivThree.addEventListener('click', changeColor);
    document.body.appendChild(newDivThree);

}

function changeColor(newDiv, newDivTwo, newDivThree){
    if (newDiv.style.backgroundColor =='rgb(255, 0, 0)' && newDivTwo.style.backgroundColor =='rgb(255, 0, 0)'){
        newDivThree.style.backgroundColor ='rgb(255, 0, 0)';
    }
    else if (newDiv.style.backgroundColor =='rgb(255, 0, 0)' && newDivTwo.style.backgroundColor=='rgb(0, 0, 255)'){
        newDivThree.style.backgroundColor='rgb(255, 0, 255)';
    }

}
Aaron Horne
  • 61
  • 10
  • Take a look at the second code snippet in the top answer to this question: http://stackoverflow.com/questions/29951130/ Is that the sort of thing you want to make? – Michael Laszlo May 04 '15 at 17:59

1 Answers1

1

well to get you started, you don't need a loop to only do one iteration. Second, the function is expecting values to be sent over in the parameter list, but they are never getting sent. So it's seeing those as null and then you are trying to reference properties of null, so that's why you get that error.

var newDiv = document.createElement("div");
newDiv.addEventListener('click', changeColor);
document.body.appendChild(newDiv);

var newDivTwo = document.createElement("div");
newDivTwo.addEventListener('click', changeColor);
document.body.appendChild(newDivTwo);

var newDivThree = document.createElement("div");
newDivThree.addEventListener('click', changeColor);
document.body.appendChild(newDivThree);

function changeColor(){
    if (newDiv.style.backgroundColor =='rgb(255, 0, 0)' && newDivTwo.style.backgroundColor =='rgb(255, 0, 0)'){
        newDivThree.style.backgroundColor ='rgb(255, 0, 0)';
    }
    else if (newDiv.style.backgroundColor =='rgb(255, 0, 0)' && newDivTwo.style.backgroundColor=='rgb(0, 0, 255)'){
        newDivThree.style.backgroundColor='rgb(255, 0, 255)';
    }

}

You should be able to work on your color changing logic now

Seth T
  • 305
  • 1
  • 10