I'm trying to change the body background color that flashes by id change using recursive function, that functions by even/odd condition check and continues by incrementing the supplied parameter after each excution. I'm executing this function using DOM level zero event handler on-click. I'm seeing some weird results.
Here is the html/css with its javascript code. Also see the comments that were put in the code, that explains other weird problems.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#style1 {
background:red;
}
#style2 {
background:black;
}
</style>
</head>
<body>
<input name="" type="button" value="Style Changer" id="button1" />
<script>
var button = document.getElementById("button1");
var background = function (y) {
alert(y); // results of background change or rest of the code only works if this alert is there which is what I don't really understand
if(y % 2 === 0) {
alert(y); // weird result: this shows 2 for no reason.
document.body.id = "style1"; // value supplied to the parameter y is 1 but taking the css property that suppose to take when the y is an even number.
var y = y + 1;
background(y);
} // End If
else {
document.body.id = "style2";
var y = y + 1;
background(y);
} //End Else
} // End of function
button.onclick = function () {
var x = 1;
background(x);
}
// Another big problem: Last but not least remove all the alert function and it will not work as it worked when there were alerts in the code.
</script>
</body>