Here’s an example JavaScript code that generates random background and text colors for an HTML document:
<!DOCTYPE html>
<html>
<head>
<title>Random Color Change</title>
<style>
body {
background-color: #ffffff;
color: #000000;
font-size: 36px;
text-align: center;
padding-top: 100px;
}
</style>
</head>
<body>
<script>
function getRandomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function changeColors() {
var backgroundColor = getRandomColor();
var textColor = getRandomColor();
document.body.style.backgroundColor = backgroundColor;
document.body.style.color = textColor;
}
setInterval(changeColors, 1000);
</script>
</body>
</html>
The getRandomColor function generates a random hexadecimal color code, and the changeColors function sets the background and text colors of the HTML body element to random colors using the style property. The setInterval function is used to call the changeColors function every second, causing the colors to change randomly over time.

Leave a Reply