Reputation:
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
Reputation: 3626
Try something like this
document.getElementById("id of body").setAttribute("style","background-url:'images/SecondBackground.png'")
Upvotes: 0
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
Reputation: 10012
You were close, but you need .body as well.
document.body.style.background = "url('images/SecondBackground.png')";
Upvotes: 0
Reputation: 2325
Use
document.body.style.backgroundImage = "url('images/SecondBackground.png')";
Upvotes: 2