Alex Rivera | Logout

How to change HTML background with JavaScript Function?

Asked 2010-04-11T21:02:16.780
12

How to change HTML background with JavaScript Function? I need some simple function to change background from one image to another?

Edit
Report

1 Answer

1

Very simple like this:

function changeBGImage(){
     document.body.background = "image.jpg";
}

UPDATE

Or if you wanna use CSS (more elegant solution) with it, you could use:

<style>
.bg1 {
background-image: url(images/my_image.jpg); 
}
</style>

<body id="page_body">

<script>
function changeBGImage(whichImage){
     document.getElementById('page_body').className="bg"+whichImage;
}
</script>
answered 2010-04-11T21:04:31.907

Your Answer