Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Change Main Background Image Re: Change Main Background Image

#56735
AshtonSanders
Participant

Hi Niplo,

Good question. Basically, you will use CSS to assign the background like normal, and then use Javascript to change the background image.

Here’s the basic Javascript code for changing an element’s background:

Code:
document.getElementById(‘randomID’).style.backgroundImage = “url(images/myimage.gif)”;

Then you could use a "onclick" attribute to run the code. Simple example:

Code:

Note: that you will need to replace randomID with the ID of the item you will be replacing. You may need to give an ID to <body> if that is where you background is applied to.

-Ashton Sanders
-www.WebssitesinaFlash.com