0

This is my current code, I'm trying to set the background of my site to a rainbow gradient however this code isn't working, however I know its being called as the alert goes off on the page? How would I go about fixing this?

var date = new Date();
var hours = date.getHours();
var day = date.getDay();
var seconds = date.getSeconds();
var minutes = date.getMinutes();
var month = date.getMonth();

if (month == 4) {
    document.getElementById("container").style.backgroundColor = `linear-gradient(red, yellow, green)`;
    alert("test");
}
3
  • 1
    Use style.backgroundImage Commented May 31, 2021 at 21:20
  • Does this answer your question? Adding CSS Gradient as Background Image Programatically Commented May 31, 2021 at 21:22
  • From MDN: creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the <gradient> data type, which is a special kind of <image> Commented May 31, 2021 at 21:25

1 Answer 1

0

Use el.style.background not backgroundColor.

See examples here: MDN linear-gradient()

var date = new Date();
var hours = date.getHours();
var day = date.getDay();
var seconds = date.getSeconds();
var minutes = date.getMinutes();
var month = date.getMonth();

if (month == 4) {
    document.getElementById("container").style.background = `linear-gradient(red, yellow, green)`;
}
#container {
  width: 500px;
  height: 500px;
}
<div id="container"></div>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.