How do I change the background url in javascript after using background-size: cover?

579 views Asked by At

This is the CSS part (using background-size: cover):

#container>div {
  background: url("../static/opti2.jpg");
  background-size: cover;
  background-attachment: fixed;
  transition: all 0.5s ease-in;
}

What I want to do is change the URL background using Javascript.

1

There are 1 answers

2
Matthew M. On

As mentioned in a comment above, use this code, replacing the URL with the new URL.

<script>

document.getElementById("container").style.backgroundImage = "url(..static/opti22.jpg)";

</script>