-2

window.onload = myFunction;

function myFunction() {

  document.getElementById('banner').style.backgroundImage = "url (../img/Background2.jpg)";

//This function is to change the backgroundimg on launch }

#banner {
  background-image: url(../img/Background2.jpg);
}
<div id="banner">
Vikas Jadhav
  • 4,597
  • 2
  • 19
  • 37

1 Answers1

0

Your issue is the space between url and ( in the JS.

So the line:

document.getElementById('banner').style.backgroundImage = "url (../img/fff.png)";

Needs to become:

document.getElementById('banner').style.backgroundImage = "url(../img/fff.png)";

Here's a working example:

window.onload = myFunction;

function myFunction() {

  document.getElementById('banner').style.backgroundImage = "url(https://via.placeholder.com/350x350)";

//This function is to change the backgroundimg on launch
}
#banner {
  width: 500px;
  height: 500px;
  background-image: url('https://via.placeholder.com/350x150');
}
<div id="banner"></div>
  • 1
    I want to thank you a 10000000 times. This worked thank you very much. I will accept as answer when I can. –  Sep 28 '18 at 10:47
  • Spaces aren't issue, doesn't matter how many, the point is in the height. – VXp Sep 28 '18 at 10:50
  • @Vxp - It doesn't work with the space there. I tested it before I posted the answer. –  Sep 28 '18 at 10:51
  • Only if I add spaces to both, JS and CSS, I don't get the image, otherwise I do. – VXp Sep 28 '18 at 10:52