user818700
user818700

Reputation:

Change document background image in javascript function

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?

Upvotes: 0

Views: 1751

Answers (4)

Aravindhan
Aravindhan

Reputation: 3626

Try something like this

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

Upvotes: 0

Barrie Reader
Barrie Reader

Reputation: 10713

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!

Upvotes: 1

Ryan McDonough
Ryan McDonough

Reputation: 10012

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

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

Upvotes: 0

Amol Kolekar
Amol Kolekar

Reputation: 2325

Use

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

Upvotes: 2

Related Questions