I need to have the divs in rows that alternate colors.
I'm trying to get this working in a regular browser before I try to implement it in our back/front end. So here is the test code I have. I can't for the life of me find out why this isn't working. I have tried the onload with the body instead of in the script tag and I have tried linking to an external jS. I figured for an example code this would be the easiest way to do it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="HTML5">
<meta name="author" content="Site">
<style>
.testclass {
width: 100%;
height: 10px;
background-color: #fdc345;
}
</style>
</head>
<body>
<div class="testclass"></div>
<div class="testclass"></div>
<div class="testclass"></div>
<div class="testclass"></div>
<script>
function isEven(value) {
if (value % 2 == 0) {
return true;
} else {
return false;
}
}
function setColors() {
var userList = document.getElementsByClassName("testclass");
var i;
for (i = 0 i < userList.length; i++) {
if (isEven(i) == true) {
userList[i].style["background-color"] = "red";
/* I also tried document.getElementsByClassName("testclass")[i].style.backgroundColor = "red" */
} else {
userList[i].style["background-color"] = "blue";
}
}
}
window.onload = setColors;
</script>
</body>
</html>
What am I missing here?