0

I want to change the html documents background image in a javascript function but I'm having some difficulty with it. This is what I have:

function changeBgImage(){

    document.background = "url('images/SecondBackground.png')";

}

But as I said, not working.. Any ideas?

4 Answers 4

2

Use

document.body.style.backgroundImage = "url('images/SecondBackground.png')";
Sign up to request clarification or add additional context in comments.

Comments

1
function changeBGImage() {
  document.body.style.backgroundImage = "url(http://images.cheezburger.com/completestore/2010/7/9/fe8e91c5-c3f1-40cf-a034-983e8683ba73.jpg)";
};

That'll do it!

Comments

0

You were close, but you need .body as well.

document.body.style.background = "url('images/SecondBackground.png')";

Comments

0

Try something like this

              document.getElementById("id of body").setAttribute("style","background-url:'images/SecondBackground.png'")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.